Results 1 to 2 of 2

Thread: how to sort

  1. #1
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default how to sort

    what is the way to sort alphabetically strings from an array?

    (for example lets say we have the strings "Apple", "Key" and "Zebra" in an array of 5 elements.)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default Sorting Strings

    String implements the Comparable interface so we can use the compareTo method for sorting. See first paragraph and "Classes Implementing Comparable" table at Object Ordering for bird's–eye view.
    Java Code:
    public class Sorting {
        public static void main(String[] args) {
            String[] array = { "five", "Apple", "Zebra", "Key", "elements" };
            sort(array);
            print(array);
            sortIgnoreCase(array);
            print(array);
        }
    
        private static void sort(String[] strs) {
            for(int j = 0; j < strs.length; j++) {
                String minValue = strs[j];
                int minIndex = j;
                for(int k = j+1; k < strs.length; k++) {
                    if(strs[k].compareTo(minValue) < 1) {
                        minValue = strs[k];
                        minIndex = k;
                    }
                }
                if(minIndex != j) {
                    // found a lower value - swap elements
                    String temp = strs[j];
                    strs[j] = minValue;
                    strs[minIndex] = temp;
                }
            }
        }
        
        private static void sortIgnoreCase(String[] strs) {
            for(int j = 0; j < strs.length; j++) {
                String minValue = strs[j].toLowerCase();
                int minIndex = j;
                for(int k = j+1; k < strs.length; k++) {
                    if(strs[k].toLowerCase().compareTo(minValue) < 1) {
                        minValue = strs[k].toLowerCase();
                        minIndex = k;
                    }
                }
                if(minIndex != j) {
                    // found a lower value - swap elements
                    String temp = strs[j];
                    strs[j] = minValue;
                    strs[minIndex] = temp;
                }
            }
        }
    
        private static void print(String[] strs) {
            for(int j = 0; j < strs.length; j++) {
                System.out.print(strs[j]);
                if(j < strs.length-1)
                    System.out.print(", ");
            }
            System.out.println();
        }
    }

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  2. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM
  3. sort
    By Camden in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 01:11 AM
  4. Heap Sort
    By kesav2005 in forum Advanced Java
    Replies: 1
    Last Post: 11-13-2007, 11:40 AM
  5. how to sort 2 tables
    By valery in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •