简体   繁体   中英

How to sort the Object containing list in asc order

i want to get the data from list in asc order..

List<Region> region = (List<Region>) model.get("region");

if i am using Collection.sort(region);

then showing this, The method sort(List) in the type Collections is not applicable for the arguments (List)

You need to define the class Region as Comparable or you need to pass an additional parameter of type Comparator to the sort method.

Without a Comparator the signature is

public static <T extends Comparable<? super T>> void sort(List<T> list)[1]

and the javadoc explicitly said that the class must implements Comparable:

Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface . Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

in this case you need to modify your code as follow:

public class Region implements Comparable {

    public int compareTo(Region o) {
       // Add the logic to compare regions here.
    }

    ... 
}

The second version with a Comparator has the following signature :

public static <T> void sort(List<T> list, Comparator<? super T> c)

and all the elements must be comparable using the passed Comparator:

Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

In this case you need to change your code directly calling the sort method as follow:

Collections.sort(regionList, (Region r1, Region r2)-> {
    // Add compare logic here
});

Note: there is an additional little mistake in your code the class to use is Collections (with an s) and not the interface Collection. I also changed the name of the list from region to regionList (eventually name it regions) because it is not only a single element but it is a list.

Custom comparator in a line for your code,

List<Region> region = (List<Region>) model.get("region");
Collections.sort(region, new Comparator<Region>() {
 @Override
 public int compare(Region a1, Region a2) {
    return a1.getType().compareToIgnoreCase(a2.getType()); //You can alter this condition based on your condition to compare
 }
});

You can leave the sorting to the database query.

As it seems the values should be unique, you could also have as model a SortedSet , implementing class: TreeSet .

i done with it...

Collections.sort(region, new Comparator<Region>() {
            public int compare(Region obj1, Region obj2) {
                return obj1.getStrRegion().compareTo(obj2.getStrRegion());
            }
        });

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