How to sort ArrayListLong in decreasing order?
Here's one way for your list
:
list.sort(null);
Collections.reverse(list);
Or you could implement your own Comparator
to sort on and eliminate the reverse step:
list.sort((o1, o2) -> o2.compareTo(o1));
Or even more simply use Collections.reverseOrder()
since you're only reversing:
list.sort(Collections.reverseOrder());
How to sort a List/ArrayList?
Collections.sort(testList);
Collections.reverse(testList);
That will do what you want. Remember to import Collections
though!
Here is the documentation for Collections
.
How to sort ArrayList in descending order
You should sort your movieList
, assuming Movie contains genre String field, below is some quick example to sort it:
Comparator<Movie> comparator = new Comparator<Movie>() {
@Override
public int compare(Movie movie, Movie t1) {
return movie.genre.compareTo(t1.genre);
}
};
// ordered by genre
Collections.sort(movieList, comparator);
// Reverse order by genre
Collections.sort(movieList, Collections.reverseOrder(comparator));
How to sort arrayListObject by object attribute long in Java
Your comparator should look similar to this
class TweetComparator implements Comparator<Tweet> {
@Override
public int compare(Tweet t1, Tweet t2) {
return Long.compare(t1.getMillis(), t2.getMillis());
}
}
note that static int Long.compare
is since Java 7
How to sort ArrayListArrayListInteger in descending order Java
This is how you can achieve it with an Arraylist of strings.
ArrayList<String> list = new ArrayList<String>();
list.add("21,10");
list.add("19,25");
list.add("32,5");
Once you have your list, you can simply use Collections.sort()
method.
Collections.sort(list);
For reverse order simply add another argument to the sort method.
Collections.sort(list, Collections.reverseOrder());
Print the list to verify.System.out.println(list);
Here is a running code example.
https://onecompiler.com/java/3y7ahcm6x
How to Sort Date in descending order From Arraylist Date in android?
Create Arraylist<Date>
of Date class. And use Collections.sort()
for ascending order.
See sort(List<T> list)
Sorts the specified list into ascending order, according to the natural ordering of its elements.
For Sort it in descending order See Collections.reverseOrder()
Collections.sort(yourList, Collections.reverseOrder());
Related Topics
Nextdouble() Throws an Inputmismatchexception When I Enter a Double
How to Compile and Deploy a Java Class at Runtime
Why Static Fields Are Not Initialized in Time
If(False) VS. While(False): Unreachable Code VS. Dead Code
"Comparison Method Violates Its General Contract!" - Timsort and Gridlayout
Abstractmethoderror Using Uribuilder on Jax-Rs
How to Check the Bitness of My Os Using Java? (J2Se, Not Os.Arch)
How to Specify Correctly Codebase and Archive in Java Applet
This: Cannot Use This in Static Context
Generate Java Classes from .Xsd Files...
Peer Not Authenticated While Importing Gradle Project in Eclipse
Question About "Java Concurrency in Practice" Example
How to Find Out What Type Each Object Is in a Arraylist<Object>