Java: sort a String array, whose strings represent int

后端 未结 8 1160
北恋
北恋 2020-12-16 14:09

I have String[] array like

{\"3\",\"2\",\"4\",\"10\",\"11\",\"6\",\"5\",\"8\",\"9\",\"7\"}

I want to sort it in numerical ord

相关标签:
8条回答
  • 2020-12-16 14:38

    Your desired output contains the numerical order of corresponding integers of your strings. So simply you cannot avoid conversion of strings to integers. As an alternative comparator to vikingsteve's you can use this:

    Arrays.sort(array, new Comparator<String>() {
        @Override
        public int compare(String str1, String str2) {
            return Integer.parseInt(str1) - Integer.parseInt(str2);
        }
    });
    
    0 讨论(0)
  • 2020-12-16 14:41

    If all elements if your String array represent numbers, and if the numbers are always positive, then there is a simple way to sort numerically without a limit to the value of the number.

    This is based on the fact that a number with a larger number of digits is, in that case, always higher than a number with a smaller number of digits.

    You first compare the number of digits, and then (only if the number of digits is the same) you compare the value alphabetically:

    Arrays.sort(array,
                Comparator.comparing(String::length).thenComparing(Function.identity()));
    
    0 讨论(0)
  • 2020-12-16 14:44

    Try a custom Comparator, like this:

        Arrays.sort(myarray, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
            }
        });
    

    Hope you like it!

    0 讨论(0)
  • 2020-12-16 14:44

    I found this article about sorting strings by numeric sorting also for strings that may or may not contain numbers:

    The Alphanum Algorithm

    There is a Java implementation example linked from the article. With that class you should be able to sort your arrays numerically like this:

    Arrays.sort(myarray, new AlphanumComparator());
    
    0 讨论(0)
  • 2020-12-16 14:47

    in jdk8, you can write this code with lambda.

            List<String> list = Arrays.asList("3", "2", "4", "10", "11", "6", "5", "8", "9", "7");
            list.sort(Comparator.comparingInt(Integer::valueOf));
            list.forEach(System.out::println);
    

    especially such as input

    String[]{"3.b", "2.c", "4.d", "10.u", "11.a", "6.p", "5.i", "8.t", "9.e", "7.i"}
    

    you can use string.subString to chose which value is you really want to sort.
    like

     files.sort(Comparator.comparingInt(a -> Integer.valueOf(a.substring(0, a.indexOf(".")))));
    
    0 讨论(0)
  • 2020-12-16 14:49

    I think by far the easiest and most efficient way it to convert the Strings to ints:

    int[] myIntArray = new int[myarray.length];
    
    for (int i = 0; i < myarray.length; i++) {
        myIntArray[i] = Integer.parseInt(myarray[i]);
    }
    

    And then sort the integer array. If you really need to, you can always convert back afterwards:

    for (int i = 0; i < myIntArray.length; i++) {
        myarray[i] = "" + myIntArray[i];
    }
    

    An alternative method would be to use the Comparator interface to dictate exactly how elements are compared, but that would probably amount to converting each String value to an int anyway - making the above approach much more efficient.

    0 讨论(0)
提交回复
热议问题