简体   繁体   中英

How should I sort an ArrayList by some integers contained in it?

This is obviously not my work, but an example of it. I've been working for over two hours now on using Comparable, compareTo, compare, Collections, sort, etc. This does not involve sorting it Strings attached to them, but the first numbers should stick with their respective words. The ArrayList has to stay intact, but I've exhausted all other possibilities to the extent of what I know.

    6 Worda
    8 Wordb
    7 Wordc
    20 Wordd
    2 Worde
    5 Wordf
    1 Wordg
    10 Wordh
    1 Wordi
    2 Wordj

The new ArrayList would be something like:

    1 Wordi
    1 Wordg
    2 Wordj
    2 Worde
    5 Wordf
    6 Worda
    8 Wordb
    7 Wordc
    10 Wordh
    20 Wordd
  1. Create a class with fields to hold the information of each line (here an int and a String),
  2. make the class Comparable with itself. For instance if the class is called MyClass, have it implement Comparable<MyClass>
  3. give it a decent compareTo(...) method
  4. In this method you should sort by the number fields first and return a value if the numbers are not equal.
  5. and then by String second (if the two numbers are equal).

You state,

This is obviously not my work, but an example of it...

If you need more specific help, consider posting your actual code and the actual data that it is supposed to hold.


Edit You post:

public int compareTo(Team o) { 
   if (this.apps >= o.apps) { 
     return this.apps; 
   } else { 
     return o.apps; 
   } 
} 

Please explain this if you can.


For example

// assuming Team has an int field, score and a String field, name
public int compareTo(Team o) { 
  if (Integer.compare(score, o.score) != 0) {
    // the scores are not the same, so return the comparison
    return Integer.compare(score, o.score)
  } else {
    // the scores are the same, so compare the Strings:
    return name.compareTo(o.name);
  } 
} 

Another way of sorting besides @Hovercraft answer is. Instead of defining natural ordering (with Comparable) create your own sorting strategy (using Comparator).

Create a class that holds your data

public class MyClass{

private String s;
private Integer id;
public static final Comparator MY_COMPARATOR = new MyComparator();

public MyClass(String s, Integer id){
    this.s=s;
    this.id=id;
}

@Override
public String toString(){
  return "ID :"+id+" property: "+s;
}

//Add getter&setter if you need    

//static nested class
private static class MyComparator implements Comparator<MyClass>{
      @Override
      public int compareTo(MyClass c, MyClass c2){
           //check possible nullPointerException
           int result = c.id.compareTo(c2.id);
           if(result == 0){
              return c.s.compareTo(c2.s);
           }
           return result;
      }

}

}

Then in client code

List<MyClass> list = new ArrayList<>();
 //add data to the list

 //print it before sort
 System.out.println(list); 
 Collections.sort(list,MyClass.MY_COMPARATOR);
 //print it after sorting
 System.out.println(list);

From your example it appears that you want to sort each element using the rule:

  1. Lowest number
  2. if numbers are equal, then Highest String (ie sort descending)

So your comparable should if in it something like (pseudocode)

if (me (number) < other (number) then me is less than
else if (me (number > other <number the me is greater than
else if (me (word) > other (word) then me is less than
else if (me (word) < other (word) then me is greater than
else me equals other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM