Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can any one tell me how to sort this out pardon the pun

C#
private void sort( ){

             String[] names = (String[]) members.toArray();
             sortStringExchange (names);
             for ( int k = 0;  k < members.size();  k++ )
            System.out.println(names[k] );
      }

      public static void sortStringExchange( String  x [ ] )
      {
            int i, j;
            String temp;

            for ( i = 0;  i < x.length - 1;  i++ )
            {
                for ( j = i + 1;  j < x.length;  j++ )
                {
                         if ( x [ i ].compareToIgnoreCase( x [ j ] ) > 0 )
                          {                                             // ascending sort
                                      temp = x [ i ];
                                      x [ i ] = x [ j ];    // swapping
                                      x [ j ] = temp;

                           }
                   }
             }
      }
Posted
Comments
PIEBALDconsult 24-Apr-14 12:32pm    
What seems to be the problem?
paul power 24-Apr-14 12:36pm    
I have an Arraylist of members and im trying to convert them to strings to sort them alphabetically

The idea is to use the custom comparator, which should be some class implementing the interface Comparable<T>, where T should be the the type of the element of your list (or, possibly, the common base type of the runtime types of the elements of the list, abstract or not). Please see:
http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html[^].

You will find some code sample here: http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property[^].

Now, this makes no sense and won't cast:
Java
String[] names = (String[])members.toArray();

In your code, you should compare strings representing elements, not lists or arrays of elements. Instead, you should use java.lang.Object.toString() which you could override in your element class(es). Please see:
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html[^].

—SA
 
Share this answer
 
Try this

Java
private void sort( ){

             String[] names = (String[]) members.toArray();
             // Remove this line
             //sortStringExchange (names);
             // add this using comparator as stated by Sergey
             Arrays.sort(names, new Comparator<string>() {

                @Override
                public int compare(String s1, String s2) {

                return s1.compareTo(s2);

           }
});
            
             for ( int k = 0;  k < members.size();  k++ )
              System.out.println(names[k] );
      }


Hope this helps :)

BCD
 
Share this answer
 
v2
Comments
BupeChombaDerrick 24-Apr-14 17:22pm    
In String[] names = (String[]) members.toArray();

What is members? Maybe it is the cast that is the problem.
paul power 24-Apr-14 17:34pm    
Maybe i'm going about it wrong i can get the arraylist of members and index with the following code but i have to be able to get them back in alphabetical order using a sort method and i'm not allowed to use the use of a Java lib.

public void listNames() {
for (int i = 0; i < members.size(); i++) {
Member member = members.get(i);
member.getName();
StdOut.println(i + ":" + member.getName());
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900