How to Sort an Arraylist in Java

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.

Sorting ArrayList of Arraylist String in java

Assuming your Lists in your List has Strings in the order id, name, address and number (i.e. name is at index 1), you can use a Comparator, as follows:

List<List<String>> list;
Collections.sort(list, new Comparator<List<String>> () {
@Override
public int compare(List<String> a, List<String> b) {
return a.get(1).compareTo(b.get(1));
}
});

Incidentally, it matters not that you are using ArrayList: It is good programming practice to declare variables using the abstract type, i.e. List (as I have in this code).

How to sort an ArrayList of ArrayLists (by one of the variables) in Java?

With Stream API (Java 8):

public static void main(String[] args) {
List<List<Integer>> list = new ArrayList<>();

list.add(asList(5, 10));
list.add(asList(2, 11));
list.add(asList(1, 12));
list.add(asList(8, 15));

System.out.println("sorted asc = " + list.stream()
.sorted(Comparator.comparing(o -> o.get(0)))
.collect(Collectors.toList()));

System.out.println("sorted desc = " + list.stream()
.sorted((i, j) -> -i.get(0).compareTo(j.get(0)))
.collect(Collectors.toList()));
}

private static List<Integer> asList(Integer... arr) {
return Arrays.asList(arr);
}

sorted asc = [[1, 12], [2, 11], [5, 10], [8, 15]]

sorted desc = [[8, 15], [5, 10], [2, 11], [1, 12]]

How to sort an ArrayList of type Entry Character, Integer ?

You could sort the list by the values this way:

list.sort(Comparator.comparing(Entry::getValue));

How to sort an ArrayList in Java

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.

How can I sort an arraylist based on the name and the Arraylist contains name with Initial of a person?

I strongly suggest to use a better object then String in your list.
In that case you can use a Comparator for sorting out your list, or make your object Comparable.

import java.util.ArrayList;
import java.util.Collections;

public class ComparatorExample {

static class Person implements Comparable<Person> {
private String initial;
private String surName;

public Person(final String initial, final String surName) {
this.initial = initial;
this.surName = surName;
}

public String getInitial() {
return initial;
}

public void setInitial(final String initial) {
this.initial = initial;
}

public String getSurName() {
return surName;
}

public void setSurName(final String surName) {
this.surName = surName;
}

@Override
public int compareTo(final Person o) {
return surName.compareTo(o.surName);
}

@Override
public String toString() {
return initial + ' ' + surName;
}
}

public static void main(String[] args) {
Person a = new Person("A", "John");
Person b = new Person("B", "Adam");
Person c = new Person("K", "Henry");
ArrayList<Person> list = new ArrayList<>();
list.add(a);
list.add(b);
list.add(c);

System.out.println(list);

Collections.sort(list);
System.out.println(list);

}
}

Sorting an ArrayList of arrays in java

You should use Collections.sort() together with a custom Comparator:

List<Integer[]> arrays = new ArrayList<>();

arrays.add(new Integer[]{1, 2});
arrays.add(new Integer[]{3, 4});

Collections.sort(arrays, new Comparator<Integer[]>() {
public int compare(Integer[] a, Integer[] b) {
return 1; // FIX this according to your needs
}
});

compare() above is just a stub, you should implement it according to the documentation and it could be replaced with a lambda expression. In the code above that would be: Collections.sort(arrays, (a, b) -> 1).

Sort an Arraylist without modifying the original list

Well, you don't actually need to make a deep copy unless your Comparator is mutating your objects, which it shouldn't. The easiest way is therefore to make shallow copy:

ArrayList<String> original = new ArrayList<>();
ArrayList<String> copy = new ArrayList<>(original);
copy.sort(Comparator.naturalOrder());

Replace Comparator.naturalOrder() with your actual Comparator implementation. For example, if you are comparing a member field, you can use Comparator.comparing as an easy way to create your desired Comparator.

And to answer your question: No, it's not possible without an additional data structure because on the one hand you want to alter the order of the elements, i.e. sort them, and on the other hand you want them to stay in the same order.

Sorting an ArrayList String[] in alphabetical order

The method Collections.sort is parametrised by T that means the condition <T extends Comparable<? super T>> should be satisfied. String[] doesn't meet the requirement because it doesn't extend Comparable.

Collections.<String[]>sort(new ArrayList<>());
Collections.<String>sort(new ArrayList<>());

We utilise Collections.sort(List, Comparator) when we want to sort uncomparable values.

Collections.sort(new ArrayList<>(), (String[] a1, String[] a2) -> 0);
Collections.<String[]>sort(new ArrayList<>(), (a1, a2) -> 0);

Of course, you should replace a mock comparator (String[] a1, String[] a2) -> 0 (which simply treats all elements the same) with the real one.



Related Topics



Leave a reply



Submit