简体   繁体   English

Java:以数字方式对字符串数组进行排序

[英]Java: Sort a String array in a numeric way

I have a String array that contains the following entries: 我有一个包含以下条目的String数组:

Array[0] = "70% Marc"
Array[1] = "50% Marc"
Array[2] = "100% Marc"
Array[3] = "20% Marc"

And I would like to sort this array descending. 我想对这个数组降序排序。 When I use Arrays.sort(Array) then it does sort it descending but the 100% Marc is at the bottom (because it only looks at the first character to sort it). 当我使用Arrays.sort(Array)它确实对它进行降序排序,但100% Marc位于底部(因为它只看第一个字符对其进行排序)。 I want it to be sorted like this: 我希望将其排序为:

"100% Marc"
"70% Marc"
"50% Marc"
"20% Marc"

How can I do that? 我怎样才能做到这一点?

Write your own CustomStringComparator and use it with the sort method. 编写自己的CustomStringComparator并将其与sort方法一起使用。

public class CustomStringComparator implements Comparator<String>{

    @Override
    public int compare(String str1, String str2) {

       // extract numeric portion out of the string and convert them to int
       // and compare them, roughly something like this

       int num1 = Integer.parseInt(str1.substring(0, str1.indexOf("%") - 1));
       int num2 = Integer.parseInt(str2.substring(0, str2.indexOf("%") - 1));

       return num1 - num2;

    }
}

You have to use a custom Comparator . 您必须使用自定义的Comparator Basically in your method compare() you will write the logic to order two String s. 基本上,在方法compare()您将编写对两个String排序的逻辑。

You will need a custom comparator: 您将需要一个自定义比较器:

import java.util.Comparator;

public class CustomComparator implements Comparator<String>{

    @Override
    public int compare(String firstString, String secondString) {
        int number1 = Integer.parseInt(firstString.substring(0, firstString.indexOf('%') - 1);
        int number2 = Integer.parseInt(secondString.substring(0, secondString.indexOf('%') - 1);
        return number1.compareTo(number2);
    }
}

Use this comparator with something like this: 将此比较器与以下内容一起使用:

List<String> entries = new ArrayList<String>();
entries.add("70% Marc");
entries.add("50% Marc");
entries.add("100% Marc");
entries.add("20% Marc");

CustomComparator comparator = new CustomComparator();
Collections.sort(entries, comparator);

You'd need to implement a custom Comparator<String> that handles the comparison by removing the percent sign: 您需要实现一个自定义Comparator<String>来通过删除百分号来处理比较:

public int compare(String str1, String str2) {
    Integer number1 = Integer.parseInt(str1.substring(0, str1.length - 1));
    Integer number2 = Integer.parseInt(str1.substring(0, str2.length - 1));
    return number1.compareTo(number2);
    // or use primitives, and then: return (x < y) ? -1 : ((x == y) ? 0 : 1);
    // but that's harder to read
}

The comparison itself can be done by using the Integer wrapper, the code that I pasted (taken from the wrapper), or guava's Ints.compare(int1, int2) 比较本身可以通过使用Integer包装器,我粘贴的代码(从包装器中获取)或番石榴的Ints.compare(int1, int2)

Then use Collections.sort(array, comparator) 然后使用Collections.sort(array, comparator)

It's not looking at the first character, it's sorting based on a string value as that's what you have in your array. 它不是在看第一个字符,而是根据字符串值进行排序,因为这就是数组中的内容。

I would suggest storing two fields, 我建议存储两个字段,

Array[0,0] = 50; Array [0,0] = 50; Array[0,1] = "Marc" Array [0,1] =“ Marc”

And then sorting based on the numeric field, if that's the behaviour you're after. 然后,如果要这样做,则根据数字字段进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM