1

How can i Sort a list of String array objects alphabetically.

I Set my List as follows:

List CurrentRankingsList = rankings.getRankings();

I have a List of String objects like this:

[["Dwayne","5"], ["Bill","4"], ["James","3"], ["Sally","3"]]

How can i sort this list by the Names, alphabetically whilst maintaining the List structure eg [["Bill","4"], ["Dwayne","5"], ["James","3"], ["Sally","3"]] I've tried the following but list is not getting sorted correctly

      (Updated code from original question )    
           Collections.sort(CurrentRankingsList, new Comparator<Rankable>() 
            {
            @Override
            public int compare(Rankable o1, Rankable o2) {
            // TODO Auto-generated method stub
            String testo1=o1.getObject().toString();
            String testo2=o2.getObject().toString();
            int x = o1.getObject().toString().compareTo(o2.getObject().toString());
            return  o1.getObject().toString().compareTo(o2.getObject().toString());
            }
        }) ;

On debugging the compare we get a Paul (o1.GetObject.ToString) vs Nathan(o2.GetObject.ToString)

Fearghal
  • 10,569
  • 17
  • 55
  • 97
  • Why do you use raw types ? – user2336315 Dec 12 '13 at 19:09
  • [Collections#sort](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List)) also: [Generics](http://docs.oracle.com/javase/tutorial/java/generics/) – gla3dr Dec 12 '13 at 19:10
  • http://stackoverflow.com/questions/4907683/sort-a-two-dimensional-array-based-on-one-column You will get the idea from here – Aroon Dec 12 '13 at 19:39
  • This looks very similar to JSON if you just adjust the format. See here http://stackoverflow.com/questions/15855548/how-to-sort-array-of-json-objects-in-java – Engineer2021 Dec 12 '13 at 19:42

1 Answers1

2

How can i Sort a list of String array objects alphabetically.

  • Declare list or any collection with type: ArrayList<String[]>, List<String[]> otherwise the Collection will be regarded as raw type and we will face run-time error overhead for type unsafe operation.
  • You will need to pass String[] as type to Comparator

     Collections.sort(CurrentRankingsList, new Comparator<String[]>() 
     {
        @Override
        public int compare(String[] o1, String[] o2) {
            // TODO Auto-generated method stub
                    return o1[0].compareTo(o2[0]);
         }
     }) ;
    

    Assuming that CurrentRankingsList is the list of array string.

  • For such case, more cleaner approach would be to declare a class for example, Player with two attribute(name, rank) and then sort based upon the instance of that class.

     class Player
     {
        String name;
        int rank;
    
         // your other code with constructor and getter, setter method    
      }
    
     ArrayList<Player>currentRankingsList = new ArrayList<>();
    
     Collections.sort(CurrentRankingsList, new Comparator<Player>() 
     {
        @Override
        public int compare(Player o1, Player o2) {
            // TODO Auto-generated method stub
                    return o1.getName().compareTo(o2.getName());
         }
     }) ;
    
Sage
  • 15,290
  • 3
  • 33
  • 38
  • I think CurrentRankingsList is either a List of List or a List of String[]. – user2336315 Dec 12 '13 at 19:10
  • I think you might be right but the OP's mentioning about `How can i sort this list by the Names` is quite confusing :) – Sage Dec 12 '13 at 19:13
  • That wouldn't change your code too much ^^ `public int compare(String[] o1, String []o2) {return o1[0].compareTo(o2[0]);}` – user2336315 Dec 12 '13 at 19:14
  • Hey, thanks for the answer. 1st one error'd because we were comparing an object to a string. So we tried second and debugged at the compare and see that Paul vs Nathan returns a 2. The list is then not sorted correctly, i think it does some sorting which appears random. – Fearghal Dec 12 '13 at 20:06
  • @Fearghal, could you please rephrase. Are you saying the proposed approach using `Comparator` isn't working ? – Sage Dec 12 '13 at 20:15
  • Apologies - i had capitals in the middle of some strings and didnt realize the sort would be affected by them. Fixed. Thanks a million Sage. – Fearghal Dec 14 '13 at 15:35