int[] array (sort lowest to highest)

后端 未结 11 1567
悲哀的现实
悲哀的现实 2020-12-01 13:32

So I am not sure why this is becoming so hard for me, but I need to sort high to low and low to high.

For high to low I have:

int a, b;
int temp;
int         


        
11条回答
  •  半阙折子戏
    2020-12-01 13:46

    Unless you think using already available sort functions and autoboxing is cheating:

    Integer[] arr =
        { 12, 67, 1, 34, 9, 78, 6, 31 };
        Arrays.sort(arr, new Comparator()
        {
            @Override
            public int compare(Integer x, Integer y)
            {
                return x - y;
            }
        });
    
        System.out.println("low to high:" + Arrays.toString(arr));
    

    Prints low to high:[1, 6, 9, 12, 31, 34, 67, 78]

    if you need high to low change x-y to y-x in the comparator

提交回复
热议问题