Asked  7 Months ago    Answers:  6   Viewed   26 times

I have a List of doubles in java and I want to sort ArrayList in descending order.

Input ArrayList is as below:

List<Double> testList = new ArrayList();

testList.add(0.5);
testList.add(0.2);
testList.add(0.9);
testList.add(0.1);
testList.add(0.1);
testList.add(0.1);
testList.add(0.54);
testList.add(0.71);
testList.add(0.71);
testList.add(0.71);
testList.add(0.92);
testList.add(0.12);
testList.add(0.65);
testList.add(0.34);
testList.add(0.62);

The out put should be like this

0.92
0.9
0.71
0.71
0.71
0.65
0.62
0.54
0.5
0.34
0.2
0.12
0.1
0.1
0.1

 Answers

72
Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Here is the documentation for Collections.

Tuesday, June 1, 2021
 
edorian
answered 7 Months ago
73

Use a Comparator like this:

List<Fruit> fruits= new ArrayList<Fruit>();

Fruit fruit;
for(int i = 0; i < 100; i++)
{
  fruit = new Fruit();
  fruit.setname(...);
  fruits.add(fruit);
}

// Sorting
Collections.sort(fruits, new Comparator<Fruit>() {
        @Override
        public int compare(Fruit fruit2, Fruit fruit1)
        {

            return  fruit1.fruitName.compareTo(fruit2.fruitName);
        }
    });

Now your fruits list is sorted based on fruitName.

Tuesday, June 1, 2021
 
FWH
answered 7 Months ago
FWH
86

Create an appropiate Comparator that will compare two items according to your desired criteria. Then use Collections.sort() on your ArrayList.

If at a later time you want to sort by different criteria, call Collections.sort() again with a different Comparator.

Tuesday, June 8, 2021
 
SubniC
answered 6 Months ago
44

Don't use a String when you want a Date. Use a Date. You should only transfom the Date to a String when displaying it. Otherwise, everywhere in the code, the date should of type Date. This is what allows sorting in chronological order, because dates have a natural order which is chronological.

So, once the RowItem has a startDate and an endDate, both being of type Date, you can sort a list of row items by start date using a simple comparator:

Collections.sort(rowItems, new Comparator<RowItem>() {
    @Override
    public int compare(RowItem r1, RowItem 2) {
        return r1.getStartDate().compareTo(r2.getStartDate());
    }
}); 

Also, fix your indentationof if/else blocks, because your way is really not readable:

if (aryBeginDate.equals(" ")) {
    row.setStartDate(" ");
} 
else {
    row.setStartDate(aryBeginDate.get(i).toString());
}
Wednesday, August 4, 2021
 
ranhan
answered 4 Months ago
11

You should do

Collections.sort(yourList.subList(1, yourList.size()));

Since the List.subList method returns a view of the list, modifications done by Collections.sort will affect the backing list as well.

Monday, August 23, 2021
 
timothy5216
answered 4 Months ago
46

I think what you are looking for is Collections.sort(java.util.List, java.util.Comparator). With it you can specify a custom Comparator that compares the strings based on length instead of alphabetical relationship.

Something like this:

List<String> stringList = new ArrayList<String>();

// Fill the list

Comparator<String> stringLengthComparator = new Comparator<String>()
    {
        @Override
        public int compare(String o1, String o2)
        {
            return Integer.compare(o1.length(), o2.length());
        }
    };

Collections.sort(stringList, stringLengthComparator);
Wednesday, November 17, 2021
 
Chris
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share