3

If I have a List<String[]> in which each String[] is as follows: {FirstName, LastName, Income, City} how would I go about using Java 8 lambdas to sort the List by a certain value such as income or first name?

2
  • 5
    Why don't you turn the String[] into an object, e.g. Person, and have the Person class implement Comparable? Commented May 3, 2017 at 7:30
  • 1
    Also if income is a number putting it in a string will make it not sortable (it will sort alphabetically). Commented May 3, 2017 at 7:32

2 Answers 2

5

Here's a couple examples. Replace x in the first two examples below with the index of the field you'd like to use for sorting.

Collections.sort(personList, (p1, p2) -> p1[x].compareTo(p2[x]));

or

personList.sort((p1, p2) -> p1[x].compareTo(p2[x]);

Also, I agree with @Robin Topper's comment. If lambdas are required (and you wanted to sort by first name), you could use:

Collections.sort(personList, (p1, p2) -> p1.getFirstName().compareTo(p2.getFirstName()));

or

personList.sort((p1, p2) -> p1.getFirstName().compareTo(p2.getFirstName()));

Also consider using the comparable implementation from Robin's comment and a data-structure allowing sorting.

Sign up to request clarification or add additional context in comments.

5 Comments

My suggested Person class would use getters and setters instead of public fields ;-)
Or Comparator.comparing(Person::getFirstName).
Likewise, Comparator.comparing(a -> a[x]) for the array case. In either case, Comparator.comparing ensures that both sides of the comparison use the same function, which avoids copy&paste errors and such alike.
Thank you it works wonderfully for the strings, is there any way I could parse the income element to an integer and sort it by that?
I actually managed to figure it out. I used Collections.sort(relative, (p1, p2) -> Integer.valueOf(p2[2]).compareTo(Integer.valueOf(p1[2])));
2

If you can rely on the order in this array, then as simple as:

List<String[]> list = Arrays.asList(new String[] { "eugene", "test", "300", "LA" }, 
                              new String[] { "hunter", "test2", "25", "CA" });

 List<String[]> sorted = list.stream()
            .sorted(Comparator.comparingLong(s -> Long.parseLong(s[2])))
            .collect(Collectors.toList());

    sorted.forEach(s -> System.out.println(Arrays.toString(s)));

But the general advice to create an Object from those fields is much better.

4 Comments

You don't need to create a stream for sorting, e.g. list.sort(Comparator.comparing(s -> Long.parseLong(s[2])))
@RobinTopper yup I know, the other answer shows that. thx anyway
List.of(…) requires Java 9. In Java 8, you have to stay with Arrays.asList(…). But if you use List.of, you can’t use List.sort, so using a stream is justified.
It might be beneficial to use Comparator.comparingLong(s -> Long.parseLong(s[2])), by the way, to avoid boxing overhead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.