Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting multi-type string ArrayList in Java based on Integers

I have a data Set like this:

1,JOHN,1934
2,TERENCE,1914
3,JOHN,1964
4,JOHN,1904
5,JOHN,1924
6,JOHN,1954
7,JOHN,1944
8,JOHN,1984
9,JOHN,1974
10,JOHN,1994

Which I've loaded in ArrayList of String[] from Text file like this:

ArrayList<String[]> records = new ArrayList<>();
String fileLocation = System.getProperty("user.dir");
String dataPath = fileLocation + File.separator + "boys-names.txt";
try {
    try (BufferedReader br = new BufferedReader(new FileReader(dataPath))) {
        String line;
        while ((line = br.readLine()) != null) {
            String[] values = line.split(",");
            records.add(values);
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

I wanted to sort data set in increasing year like this:

4,JOHN,1904
2,TERENCE,1914
5,JOHN,1924
1,JOHN,1934
7,JOHN,1944
6,JOHN,1954
3,JOHN,1964
9,JOHN,1974
8,JOHN,1984
10,JOHN,1994

Problem: The built-in sorting method Collections.sort(list); of ArrayList only works on single type of data. But, in my case I have string with multi-type (string-integer) and sorting should base in Integers. So, is there any way to solve this problem?

like image 892
youpilat13 Avatar asked Jul 28 '19 15:07

youpilat13


People also ask

How do you sort an ArrayList in integers?

Approach: An ArrayList can be Sorted by using the sort() method of the Collections Class in Java. This sort() method takes the collection to be sorted as the parameter and returns a Collection sorted in the Ascending Order by default.

Can we sort string and integer in Java?

Sort String in Java String in Java is immutable. There is no direct method to sort a string in Java. You can use Arrays, which has a method CharArray() that will create a char input string and using another method (Arrays. sort(char c[]) , we can easily sort.

How do you sort an ArrayList of strings?

An ArrayList can be sorted by using the sort() method of the Collections class in Java. It accepts an object of ArrayList as a parameter to be sort and returns an ArrayList sorted in the ascending order according to the natural ordering of its elements.

Can you have multiple types in an ArrayList Java?

It is more common to create an ArrayList of definite type such as Integer, Double, etc. But there is also a method to create ArrayLists that are capable of holding Objects of multiple Types.


1 Answers

Consider defining a model class for your data - let's call it MyData :

public class MyData {
    private long id;
    private String name;
    private Year year;

    //getters setters constructor
}

If the last integer value represents a year, why not use java.time.Year directly? And then you could sort this list using List::sort method and passing a comparator :

Comparator<MyData> comparator = Comparator.comparing(MyData::getYear);

myDataList.sort(comparator);
like image 162
Michał Krzywański Avatar answered Oct 07 '22 19:10

Michał Krzywański