【问题标题】:How to sort a list in ascending order of string index in java如何在java中按字符串索引的升序对列表进行排序
【发布时间】:2018-12-23 06:54:42
【问题描述】:

我在 java 中创建了一个列表,如下所示:

1   bbbb london
1.1 aaaa nyc
1.10 cccc jaipur
...
1.2 test test
1.11 test1 test1

我需要根据索引 1、1.1、1.2 等对其进行排序!这些是字符串值。

喜欢:

1   bbbb london
1.1 aaaa nyc
1.2 test test
...
1.10 cccc jaipur
1.11 test1 test1

我该怎么做?

最初索引是浮动的,但为了在列表中获得 1.10,我将索引更改为字符串,因此 Collection.sort(list) 没有按预期提供输出。

我的目的是像创建一个编号的子弹

1 Helo
 1.1 helo1
 1.2 helo2
 1.3 hello3
2 Test
 2.1 Test1
 2.2 Test2
3 World

有什么帮助吗?

【问题讨论】:

  • 我建议为主要索引和次要索引设置单独的字段。
  • @NicholasK。它只能是 1 - 1.1 - 1.2 ... 1.10 - 1.11 - 2 - 2.1 - 2.2 blah blah。这就像在两个主要点之间添加中间点。像编号的子弹
  • @TimBiegeleisen。但是,如果我创建另一个具有浮点值的索引,则 1.10 将更改为 1.1,它们将是两个 1.1,排序会出错,对吗?
  • 您可能在下面有一个可行的答案。从数据模型的角度来看,将数字存储为字符串通常是不好的。

标签: java android string


【解决方案1】:

给定列表:

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");

您可以编写自己的自定义比较器,如下所示:

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]));
    }
});

在这里,我们用" " 分割字符串并获取字符串的浮动 部分,它恰好是数组的第一个index。现在我们将它解析为一个浮点数并与第二个字符串进行比较。

如果你在java-8,你可以做一行:

Collections.sort(list, (o1, o2) -> 
      Float.compare(Float.parseFloat(o1.split(" ")[0]), Float.parseFloat(o2.split(" ")[0])));

【讨论】:

  • Collections.sort(mRoute.getWayPointList(), new Comparator&lt;WayPoint&gt;() { public int compare(WayPoint o1, WayPoint o2) { return Float.compare(Float.parseFloat(o1.getId().split(" ")[0]), Float.parseFloat(o2.getId().split(" ")[0])); } }); 实际上我试过这个仍然没有它没有排序! List 中的元素是通过一个模型 Waypoint 添加的!我可以使用该 model.getId 获取索引(id)
  • 字符串是否存储在id字段中?如果可能,请分享课程 Waypoint
  • 这个答案不起作用。然而它有4票。我的答案有效,它有 -1 票。这很有趣:)
  • @PrasadKarunagoda :这个解决方案确实有效。请继续尝试。正如 OP 在 cmets 中提到的,他可能没有正确使用它,因为有一个模型类 Waypoint。这就是我要求他尽可能分享文件的原因。同时,您可以自己尝试答案。
  • @NicholasK,我在问题中提供的输入数据上尝试了您的代码。而且它没有给出问题中预期的答案。我认为您缺少的一点是,Dillz 的要求不是“自然”排序。这就是为什么它不能作为 String 或 float 工作的原因。
【解决方案2】:

试试这个。这应该有效。主要逻辑在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;
  }
}

【讨论】:

    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2021-05-17
    • 2021-07-05
    • 2023-04-08
    • 2020-09-16
    • 2018-06-06
    相关资源
    最近更新 更多