如何将String转换为int并使用Comparator进行比较

时间:2022-10-27 22:47:34

This is how i am comparing 2 Strings value, the values are in number, It works perfectly fine until it receive a value equal or greater than 10.

这就是我比较2个字符串值的方式,值是数字,它完全正常,直到它收到等于或大于10的值。

So i need to convert these values to integer.

所以我需要将这些值转换为整数。

TXCartData class has String rowID

TXCartData类具有String rowID

This is what i was doing.

这就是我在做的事情。

Collections.sort(data, new Comparator<TXCartData>() {
    @Override
    public int compare(TXCartData lhs, TXCartData rhs) {
        return lhs.rowId.compareToIgnoreCase(rhs.rowId);
    }
});

I have tried to change rowID to integer, but its giving me an error message, as its expecting 0 or 1 in output, and 1==1 returns true of false.

我试图将rowID更改为整数,但它给出了一条错误消息,因为它在输出中期望为0或1,而1 == 1则返回true为false。

This is what i tried.

这是我试过的。

Collections.sort(data, new Comparator<TXCartData>() {
    @Override
    public int compare(TXCartData lhs, TXCartData rhs) {
        return Integer.parseInt(lhs.rowId)== Integer.parseInt(rhs.rowId);
    }
});

The data list is very large, I sort it three times by using Comparatorand get accurate result, The first 2 are strings and it sorted them accurately, but on the last one it fails.

数据列表非常大,我使用Comparatorand对其进行三次排序得到准确的结果,前两个是字符串并且它们准确地排序,但是在最后一个上它失败了。

5 个解决方案

#1


3  

pls try this

试试吧

 Collections.sort(data, new Comparator<TXCartData>() {
        @Override
        public int compare(TXCartData lhs, TXCartData rhs) {
            int n1=Integer.parseInt(lhs.rowId);
            int n2=Integer.parseInt(rhs.rowId);
            if (n1>=n2){
                return 1;
            }
            return -1;
        }
    });

#2


1  

change the return type of your compare function

更改比较函数的返回类型

public boolean compare(TXCartData lhs, TXCartData rhs) {
        return Integer.parseInt(lhs.rowId)== Integer.parseInt(rhs.rowId);
    }

#3


1  

Classes that has a natural sort order (a class Number, as an example) should implement the Comparable interface, whilst classes that has no natural sort order (a class Chair, as an example) should be provided with a Comparator (or an anonymous Comparator class).

具有自然排序顺序的类(作为示例的类Number)应该实现Comparable接口,而没有自然排序顺序的类(作为示例的类)应该与比较器(或匿名比较器)一起提供类)。

public class Number implements Comparable<Number> {
    private int value;

    public Number(int value) { this.value = value; }
    public int compareTo(Number anotherInstance) {
        return this.value - anotherInstance.value;
    }
}

public class Chair {
    private int weight;
    private int height;

    public Chair(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }
    /* Omitting getters and setters */
}
class ChairWeightComparator implements Comparator<Chair> {
    public int compare(Chair chair1, Chair chair2) {
        return chair1.getWeight() - chair2.getWeight();
    }
}
class ChairHeightComparator implements Comparator<Chair> {
    public int compare(Chair chair1, Chair chair2) {
        return chair1.getHeight() - chair2.getHeight();
    }
}

Usage:

用法:

List<Number> numbers = new ArrayList<Number>();
...
Collections.sort(numbers);

List<Chair> chairs = new ArrayList<Chair>();
// Sort by weight:
Collections.sort(chairs, new ChairWeightComparator());
// Sort by height:
Collections.sort(chairs, new ChairHeightComparator());

// You can also create anonymous comparators;
// Sort by color:
Collections.sort(chairs, new Comparator<Chair>() {
    public int compare(Chair chair1, Chair chair2) {
        ...
    }
});

#4


1  

You're doing fine just change '==' to '-' in your code. This will work ...

你做的很好,只需在代码中将'=='改为' - '即可。这将有效......

Collections.sort(data, new Comparator<TXCartData>() {
        @Override
        public int compare(TXCartData lhs, TXCartData rhs) {
        return Integer.parseInt(lhs.rowId)- Integer.parseInt(rhs.rowId);
    }
});

#5


1  

Try to use:

尝试使用:

Integer.compare(int,int) and Integer.lowestOneBit(int)

int result = Integer.lowestOneBit(Integer.compare(Integer.parseInt(number1), Integer.parseInt(number2)));

#1


3  

pls try this

试试吧

 Collections.sort(data, new Comparator<TXCartData>() {
        @Override
        public int compare(TXCartData lhs, TXCartData rhs) {
            int n1=Integer.parseInt(lhs.rowId);
            int n2=Integer.parseInt(rhs.rowId);
            if (n1>=n2){
                return 1;
            }
            return -1;
        }
    });

#2


1  

change the return type of your compare function

更改比较函数的返回类型

public boolean compare(TXCartData lhs, TXCartData rhs) {
        return Integer.parseInt(lhs.rowId)== Integer.parseInt(rhs.rowId);
    }

#3


1  

Classes that has a natural sort order (a class Number, as an example) should implement the Comparable interface, whilst classes that has no natural sort order (a class Chair, as an example) should be provided with a Comparator (or an anonymous Comparator class).

具有自然排序顺序的类(作为示例的类Number)应该实现Comparable接口,而没有自然排序顺序的类(作为示例的类)应该与比较器(或匿名比较器)一起提供类)。

public class Number implements Comparable<Number> {
    private int value;

    public Number(int value) { this.value = value; }
    public int compareTo(Number anotherInstance) {
        return this.value - anotherInstance.value;
    }
}

public class Chair {
    private int weight;
    private int height;

    public Chair(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }
    /* Omitting getters and setters */
}
class ChairWeightComparator implements Comparator<Chair> {
    public int compare(Chair chair1, Chair chair2) {
        return chair1.getWeight() - chair2.getWeight();
    }
}
class ChairHeightComparator implements Comparator<Chair> {
    public int compare(Chair chair1, Chair chair2) {
        return chair1.getHeight() - chair2.getHeight();
    }
}

Usage:

用法:

List<Number> numbers = new ArrayList<Number>();
...
Collections.sort(numbers);

List<Chair> chairs = new ArrayList<Chair>();
// Sort by weight:
Collections.sort(chairs, new ChairWeightComparator());
// Sort by height:
Collections.sort(chairs, new ChairHeightComparator());

// You can also create anonymous comparators;
// Sort by color:
Collections.sort(chairs, new Comparator<Chair>() {
    public int compare(Chair chair1, Chair chair2) {
        ...
    }
});

#4


1  

You're doing fine just change '==' to '-' in your code. This will work ...

你做的很好,只需在代码中将'=='改为' - '即可。这将有效......

Collections.sort(data, new Comparator<TXCartData>() {
        @Override
        public int compare(TXCartData lhs, TXCartData rhs) {
        return Integer.parseInt(lhs.rowId)- Integer.parseInt(rhs.rowId);
    }
});

#5


1  

Try to use:

尝试使用:

Integer.compare(int,int) and Integer.lowestOneBit(int)

int result = Integer.lowestOneBit(Integer.compare(Integer.parseInt(number1), Integer.parseInt(number2)));