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)

4

1 Answer 1

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());
         }
     }) ;
    
Sign up to request clarification or add additional context in comments.

6 Comments

I think CurrentRankingsList is either a List of List<String> or a List of String[].
I think you might be right but the OP's mentioning about How can i sort this list by the Names is quite confusing :)
That wouldn't change your code too much ^^ public int compare(String[] o1, String []o2) {return o1[0].compareTo(o2[0]);}
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, could you please rephrase. Are you saying the proposed approach using Comparator<String[]> isn't working ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.