TreeSet Custom Comparator Algo .. String Comparision

对着背影说爱祢 提交于 2019-12-23 16:52:31

问题


From the input string provided:

{ "200,400,7,1", "100,0,1,1", "200,200,3,1", "0,400,11,1", "407,308,5,1","100,600,9,1" } ,

I am adding the same in a TreeSet and want it to be sorted with the 3rd element order, so the expected output will be:

(100,0,1,1) (200,200,3,1) (407,308,5,1) (200,400,7,1) (100,600,9,1) (0,400,11,1)

But my actual output is:

(100,0,1,1)(0,400,11,1)(200,200,3,1)(407,308,5,1)(200,400,7,1)(100,600,9,1)

But since the string comparison of 11 is less than 9 but in terms of integer , 11>9 . My expected output is getting differed. Suggest me some idea to resolve the same.

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetComparator {

    public static void main(String[] args) {
        Comparator<String> comparator = new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                String aStr = a;
                String bStr = b;
                String[] splitA = aStr.split(",");
                String[] splitB = bStr.split(",");

                return splitA[2].compareTo(splitB[2]);
            }
        };

        String[] arr = { "200,400,7,1", "100,0,1,1", "200,200,3,1",
                "0,400,11,1", "407,308,5,1", "100,600,9,1" };

        TreeSet<String> ts = new TreeSet<String>(comparator);
        for (String str : arr) {
            ts.add(str);
        }

        for (String element : ts)
            System.out.print(element + " ");

    }
}

回答1:


You're sorting in a lexicographical order ("123" comes before "20"), what you need to do is convert them to integers, and then compare them:

Not:

return splitA[2].compareTo(splitB[2]);

but:

return Integer.valueOf(splitA[2]).compareTo(Integer.valueOf(splitB[2]));

However, a somewhat cleaner way would be to create a custom object holding these 4 different values and then create a Comparator that compares the 3rd value of such an object:

The following:

public class Main {
    public static void main (String[] args) {

        Comparator<CustomObject> sortOn3rdValue = new Comparator<CustomObject>() {
            @Override
            public int compare(CustomObject o1, CustomObject o2) {
                return o1.v3 < o2.v3 ? -1 : o1.v3 > o2.v3 ? 1 : 0;
            }
        };

        Set<CustomObject> objects = new TreeSet<CustomObject>(sortOn3rdValue);

        String[] arr = { "200,400,7,1", "100,0,1,1", "200,200,3,1", "0,400,11,1", "407,308,5,1", "100,600,9,1" };

        for(String a : arr) {
            objects.add(new CustomObject(a.split(",")));
        }

        for(CustomObject co : objects) {
            System.out.println(co);
        }
    }
}

class CustomObject {

    final int v1, v2, v3, v4;

    CustomObject(String[] strValues) {
        // assume strValues.lenght == 4
        v1 = Integer.valueOf(strValues[0]);
        v2 = Integer.valueOf(strValues[1]);
        v3 = Integer.valueOf(strValues[2]);
        v4 = Integer.valueOf(strValues[3]);
    }

    @Override
    public String toString() {
        return String.format("(%d,%d,%d,%d)", v1, v2, v3, v4);
    }
}

would print:

(100,0,1,1)
(200,200,3,1)
(407,308,5,1)
(200,400,7,1)
(100,600,9,1)
(0,400,11,1)


来源:https://stackoverflow.com/questions/8092777/treeset-custom-comparator-algo-string-comparision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!