英文:
How can I sort an ArrayList of arrays based on the array[0] element which is a date given in dd/mm/yyyy format?
问题
我有一个小问题,需要根据数组的第0个索引对数组列表进行排序。例如,如果我有一个包含字符串数组的数组列表,如下所示:
arr1[0] = "12/21/2018"
arr1[1] = "加利福尼亚"
arr[n] = "一些字符串"
// 并且我有另一个数组
arr2[0] = "11/21/2018"
arr1[1] = "加利福尼亚"
arr[n] = "一些字符串"。
我该如何根据数组列表中每个数组的第0个索引对数组列表进行排序?
英文:
I have a small issue in which I need to sort an arraylist of arrays based on the arrays 0th index. For example If I have an array list containing string arrays such as this:
arr1[0] = "12/21/2018"
arr1[1] = "California"
arr[n] = "some string"
// and I have another array
arr2[0] = "11/21/2018"
arr1[1] = "California"
arr[n] = "some string".
How would I go about sorting the arraylist based on the 0th index of each array in the array list?
答案1
得分: 1
不必将 String
数组放入 ArrayList
中处理,为什么不创建一个具有属性的新类呢?这个类可以实现 Comparable
接口,这样我们就可以添加一个自定义的比较函数,允许我们以自定义方式对列表进行排序。例如,您可以将这个类命名为 Location,并包含一个 LocalDate
日期(如 MadProgrammer 所提到的)和一个 String
名称。示例代码如下:
class Location implements Comparable<Location> {
// 我们实现 Comparable 接口,以便覆盖 compareTo() 方法并创建自定义的排序方法。
LocalDate date;
String name;
// 根据需要添加更多属性...
public Location(LocalDate d, String n) {
// 帮助我们创建对象的构造函数
this.date = d;
this.name = n;
}
// 我假设您正在按照数组中第一个索引中的 String 作为日期的顺序进行比较。这样我们可以轻松地进行比较,如下所示。
@Override
public int compareTo(Location other) {
return this.date.compareTo(other.date);
}
}
您可以创建一个 Location
类的 ArrayList
,定义如下:
List<Location> locs = new ArrayList<>();
然后,您可以使用 Collections.sort(List<T> list)
对其进行排序。它会使用在类中创建的比较器进行排序。
Java 是面向对象的。善用它可以避免诸如此类的棘手情况。
英文:
Instead of dealing with putting an Array
of Strings
inside an ArrayList
, why not just create a new class with properties? This class could implement Comparable
so we can add a custom compare function that would allow us to sort the list in a custom fashion. For example, your class could be called Location, and has a LocalDate
date (as mentioned by MadProgrammer), and a String
name. An example would look a bit like this:
class Location implements Comparable<Location> {
// We implement the Comparable so we can Override the compareTo() method and make a custom
// sorting method.
LocalDate date;
String name;
// Add more properties as needed...
public Location(LocalDate d, String n) {
// Constructor to help us create the object
this.date = d;
this.name = n;
}
// I'm assuming you are comparing the String that was in the first index of the array as
// Dates in chronological order. That way we can compare them easily like so.
@Override
public int compareTo(Location other) {
return this.date.compareTo(other.date);
}
}
You could create an ArrayList
of the Location
class, which would be defined as something like this:
List<Location> locs = new ArrayList<>();
Then, you can sort it using Collections.sort(List<T> list)
. It sorts it using the comparator that was made in the class.
Java is Object Oriented. Use it to your advantage to avoid sticky situations like these.
答案2
得分: 0
像Pulse一样,我正在使用java.time,这是现代Java日期和时间API:
List<String[]> arraylist = new ArrayList<>();
arraylist.add(new String[] { "12/21/2018", "California", "some string"});
arraylist.add(new String[] { "11/21/2018", "California", "some other string"});
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
Comparator<String[]> cmpr = Comparator.comparing(arr -> LocalDate.parse(arr[0], dateFormatter ));
arraylist.sort(cmpr);
arraylist.forEach(arr -> System.out.println(Arrays.toString(arr)));
给定的示例数据输出如下:
[11/21/2018, California, some other string]
[12/21/2018, California, some string]
链接1: Oracle教程:日期时间 解释了如何使用java.time。
链接2: Pulse的回答。
英文:
Like Pulse I am using java.time, the modern Java date and time API:
List<String[]> arraylist = new ArrayList<>();
arraylist.add(new String[] { "12/21/2018", "California", "some string"});
arraylist.add(new String[] { "11/21/2018", "California", "some other string"});
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu");
Comparator<String[]> cmpr = Comparator.comparing(arr -> LocalDate.parse(arr[0], dateFormatter ));
arraylist.sort(cmpr);
arraylist.forEach(arr -> System.out.println(Arrays.toString(arr)));
Output with the example data given is:
> [11/21/2018, California, some other string]
> [12/21/2018, California, some string]
Link 1: Oracle tutorial: Date Time explaining how to use java.time.
Link 2: Answer by Pulse.
答案3
得分: -1
这是一个一行代码,当数据格式符合 title,即 dd/mm/yyyy
时:
Arrays.sort(arr, comparing(a -> a[0].replaceAll("(..).(..).(.*)", "$3$2$1")));
这是一个一行代码,当数据格式符合 example,即 mm/dd/yyyy
时:
Arrays.sort(arr, comparing(a -> a[0].replaceAll("(..).(..).(.*)", "$3$1$2")));
英文:
Here's a one-liner when data is as per title, ie dd/mm/yyyy
:
Arrays.sort(arr, comparing(a -> a[0].replaceAll("(..).(..).(.*)", "$3$2$1")));
Here's a one-liner when data is as per example, ie mm/dd/yyyy
:
Arrays.sort(arr, comparing(a -> a[0].replaceAll("(..).(..).(.*)", "$3$1$2")));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论