[英]How to sort a list in ascending order of string index in java
I've created a list in java as following:我在 java 中创建了一个列表,如下所示:
1 bbbb london
1.1 aaaa nyc
1.10 cccc jaipur
...
1.2 test test
1.11 test1 test1
I need to sort it based on index 1,1.1,1.2 etc. These are string values.我需要根据索引 1、1.1、1.2 等对其进行排序。这些是字符串值。
Like:像:
1 bbbb london
1.1 aaaa nyc
1.2 test test
...
1.10 cccc jaipur
1.11 test1 test1
How can i do that?我怎样才能做到这一点?
Initially the index was float but in-order to get 1.10 in list i changed index to string so Collection.sort(list)
doesnt give output as expected.最初索引是浮动的,但为了在列表中获得 1.10,我将索引更改为字符串,因此Collection.sort(list)
不会按预期提供输出。
My purpose is like to create a numbered bullets like我的目的是创建一个编号的项目符号
1 Helo
1.1 helo1
1.2 helo2
1.3 hello3
2 Test
2.1 Test1
2.2 Test2
3 World
Any help please?有什么帮助吗?
Given the list:鉴于清单:
List<String> list = Arrays.asList("1.32 bbbb london", "21.1 aaaa nyc",
"100.10 cccc jaipur", "1.2 test test",
"1.11 test1 test1");
You can write your own custom comparator as shown below:您可以编写自己的自定义比较器,如下所示:
Collections.sort(list, new Comparator<String>() {
public int compare(String o1, String o2) {
return Float.compare(Float.parseFloat(o1.split(" ")[0]),
Float.parseFloat(o2.split(" ")[0]));
}
});
Here, we split the String by " "
and fetch the floating part of the string which happens to be the first index of the array.在这里,我们用" "
分割字符串并获取字符串的浮动部分,它恰好是数组的第一个索引。 Now we parse it to a float and compare it with the second string.现在我们将其解析为浮点数并将其与第二个字符串进行比较。
If you are on java-8
you could do it one line:如果您使用的是java-8
,则可以一行完成:
Collections.sort(list, (o1, o2) ->
Float.compare(Float.parseFloat(o1.split(" ")[0]), Float.parseFloat(o2.split(" ")[0])));
Try this.试试这个。 This should work.这应该工作。 Main logic is in compareTo
method.主要逻辑在compareTo
方法中。
import java.util.ArrayList;
import java.util.Collections;
public class Sort
{
public static void main(String[] args)
{
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry("1", "bbbb london"));
entries.add(new Entry("1.1", "aaaa nyc"));
entries.add(new Entry("1.10", "cccc jaipur"));
entries.add(new Entry("1.2", "test test"));
entries.add(new Entry("1.11", "test1 test1"));
Collections.sort(entries);
for (Entry e : entries)
{
System.out.println(e);
}
}
}
class Entry implements Comparable<Entry>
{
String index;
String value;
int major;
int minor;
Entry(String index, String value)
{
this.index = index;
this.value = value;
String[] array = index.split("\\.");
if (array.length == 2)
{
major = Integer.valueOf(array[0]);
minor = Integer.valueOf(array[1]);
}
else if (array.length == 1)
{
major = Integer.valueOf(array[0]);
}
else
{
throw new IllegalArgumentException("Invalid index : " + index);
}
}
@Override
public int compareTo(Entry otherEntry)
{
if (this.major < otherEntry.major)
{
return -1;
}
else if (this.major > otherEntry.major)
{
return 1;
}
else
{
if (this.minor < otherEntry.minor)
{
return -1;
}
else if (this.minor > otherEntry.minor)
{
return 1;
}
else
{
return 0;
}
}
}
@Override
public String toString()
{
return index + " " + value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.