📜  用Java排序

📅  最后修改于: 2021-04-23 16:21:31             🧑  作者: Mango

有两种内置的Java排序方法。

  1. Arrays.Sort()适用于也可以是原始数据类型的数组。
    // A sample Java program to demonstrate working of
    // Arrays.sort().
    // It by default sorts in ascending order.
    import java.util.Arrays;
      
    public class GFG {
        public static void main(String[] args)
        {
            int[] arr = { 13, 7, 6, 45, 21, 9, 101, 102 };
      
            Arrays.sort(arr);
      
            System.out.printf("Modified arr[] : %s",
                              Arrays.toString(arr));
        }
    }
    

    输出:

    Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]
    
  2. Collections.sort()适用于对象集合,例如ArrayList和LinkedList。
    // Java program to demonstrate working of Collections.sort()
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Create a list of strings
            ArrayList al = new ArrayList();
            al.add("Geeks For Geeks");
            al.add("Friends");
            al.add("Dear");
            al.add("Is");
            al.add("Superb");
      
            /* Collections.sort method is sorting the
            elements of ArrayList in ascending order. */
            Collections.sort(al);
      
            // Let us print the sorted list
            System.out.println("List after the use of"
                               + " Collection.sort() :\n" + al);
        }
    }
    

    输出:

    List after the use of Collection.sort() :
    [Dear, Friends, Geeks For Geeks, Is, Superb]
    
  • Java在sort()中使用哪种排序算法?
    以前,Java的Arrays.sort方法使用Quicksort来存储基元数组,并使用Merge sort来处理对象数组。在Java的最新版本中,Arrays.sort方法和Collection.sort()使用Timsort。
  • 默认情况下,哪个排序顺序完成?
    默认情况下,它按升序排序。
  • 如何按降序对数组或列表进行排序?
    可以在Collections.reverseOrder()的帮助下完成。

    例子:

    1. 对于Arrays.sort()
      // A sample Java program to sort an array
      // in descending order using Arrays.sort().
      import java.util.Arrays;
      import java.util.Collections;
        
      public class GFG {
          public static void main(String[] args)
          {
              // Note that we have Integer here instead of
              // int[] as Collections.reverseOrder doesn't
              // work for primitive types.
              Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
        
              // Sorts arr[] in descending order
              Arrays.sort(arr, Collections.reverseOrder());
        
              System.out.printf("Modified arr[] : %s",
                                Arrays.toString(arr));
          }
      }
      

      输出:

      Modified arr[] : [100, 45, 21, 13, 9, 7, 6, 2]
      
    2. 对于Collections.sort()
      // Java program to demonstrate working of Collections.sort()
      // to descending order.
      import java.util.*;
        
      public class GFG {
          public static void main(String[] args)
          {
              // Create a list of strings
              ArrayList al = new ArrayList();
              al.add("Geeks For Geeks");
              al.add("Friends");
              al.add("Dear");
              al.add("Is");
              al.add("Superb");
        
              /* Collections.sort method is sorting the
              elements of ArrayList in ascending order. */
              Collections.sort(al, Collections.reverseOrder());
        
              // Let us print the sorted list
              System.out.println("List after the use of"
                                 + " Collection.sort() :\n" + al);
          }
      }
      

      输出:

      List after the use of Collection.sort() :
      [Superb, Is, Geeks For Geeks, Friends, Dear]
      
  • 如何仅对子数组排序?
    例子:
    // A sample Java program to sort a subarray
    // using Arrays.sort().
    import java.util.Arrays;
      
    public class GFG {
        public static void main(String[] args)
        {
            // Our arr contains 8 elements
            int[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
      
            // Sort subarray from index 1 to 4, i.e.,
            // only sort subarray {7, 6, 45, 21} and
            // keep other elements as it is.
            Arrays.sort(arr, 1, 5);
      
            System.out.printf("Modified arr[] : %s",
                              Arrays.toString(arr));
        }
    }
    

    输出:

    Modified arr[] : [13, 6, 7, 21, 45, 9, 2, 100]
    
  • 如何用Java编写自己的排序函数?
    请参阅Java程序以进行快速排序,合并排序,插入排序,选择排序,堆排序,冒泡排序


    如何对用户定义的数据类型的对象进行排序?

    请Java的Java和Collections.sort()是指Arrays.sort()的例子。