在java中对数字字符串间隔进行排序 [英] Sorting numeric String interval in java

查看:51
本文介绍了在java中对数字字符串间隔进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Person 类,其中包含一些 Person 和详细信息,如姓名、年龄范围.
年龄区间为{"0-5"、"6-10"、"11-30"、"31-45"、"46-50"、"50-100"、"100-110"};

我有一个带有 nameageBand 字符串间隔的 Person 类,它是参数化的构造函数、getter、setter.

class Person {字符串名称;字符串年龄带;//说它是字符串0-50",我在创建一个人时传入了构造函数.//获取者//二传手}

class TestAgeBand {公共静态无效主(字符串参数[]){ArrayList<人>person = new ArrayList();Person p1 = new Person("Mike1", "0-5");Person p2 = new Person("Mike2", "6-10");Person p3 = new Person("Mike3", "11-30");Person p4 = new Person("Mike4", "31-45");Person p5 = new Person("Mike5", "50-100");Person p6 = new Person("Mike6", "46-50");Person p7 = new Person("Mike7", "100-110");人.添加(p1);//将所有人员添加到列表中.}}

这是我用我的代码对间隔进行排序的操作.我需要根据增加的间隔对人进行排序.我正在使用 Treemap 对间隔进行排序.

MapageBandMap = new TreeMap(){for(Person p: 人) {ageBandMap.put(p.ageBand, p.name);}}

当我打印间隔键集时,我得到

输出:

<块引用>

[0-5, 100-110, 11-30, 31-45, 46-50, 50-100, 6-10]

我不需要的.我需要这样排序的间隔:

<块引用>

[0-5, 6-10, 11-30, 31-45, 46-50, 50-100, 100-110]

解决方案

尝试拆分你的 ageBand 字符串并将其转换为 Integer,这样会更容易排序.

person.stream().sorted(Comparator.comparing(element -> Integer.parseInt(element.getAgeBand().split("-")[0]))).collect(Collectors.toList());

如果您不想使用 Java 8,您可以使用 Collections.sort() 方法来实现.

 Collections.sort(person, new Comparator() {@覆盖公共 int 比较(人 o1,人 o2){return Integer.parseInt(o1.getAgeBand().split("-")[0]) - Integer.parseInt(o2.getAgeBand().split("-")[0]);}});

I'm having an Person class with some Person and there details as there name, age band.
The ageband interval is {"0-5", "6-10", "11-30","31-45", "46-50","50-100", "100-110"};

I'm having a Person class with name , ageBand String interval and it's parameterised constructor, getters, setters.

class Person {
    String name;
    String ageBand; //say it is string "0-50" which i pass in constructor while creating a person.
    //getters
    //setters
}

class TestAgeBand {
    public static void main(String args[]) {
        ArrayList<Person> person = new ArrayList<Person>();

        Person p1 = new Person("Mike1", "0-5");   
        Person p2 = new Person("Mike2", "6-10");
        Person p3 = new Person("Mike3", "11-30");   
        Person p4 = new Person("Mike4", "31-45");   
        Person p5 = new Person("Mike5", "50-100");   
        Person p6 = new Person("Mike6", "46-50"); 
        Person p7 = new Person("Mike7", "100-110");

        person.add(p1);
        //adding all persons to list.
    }
}

Here's what I'm doing with my code to sort the interval. I need to sort persons according to increasing intervals. I'm using Treemap to sort the intervals.

Map<String, Person> ageBandMap = new TreeMap<String, Person>(){
    for(Person p: person) {
        ageBandMap.put(p.ageBand, p.name);
    }
}

When I print interval keyset, I get

Output:

[0-5, 100-110, 11-30, 31-45, 46-50, 50-100, 6-10]

Which I don't need. I need intervals sorted like this:

[0-5, 6-10, 11-30, 31-45, 46-50, 50-100, 100-110]

解决方案

Try splitting the your ageBand string and converting it into an Integer, it will be easier to sort.

person.stream().sorted(Comparator.comparing(element -> Integer.parseInt(element.getAgeBand().split("-")[0])))
            .collect(Collectors.toList());

If you don't want to use Java 8, you can do it with Collections.sort() method.

 Collections.sort(person, new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            return Integer.parseInt(o1.getAgeBand().split("-")[0]) - Integer.parseInt(o2.getAgeBand().split("-")[0]);
        }
    });

这篇关于在java中对数字字符串间隔进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆