首页 > 代码库 > Arrays排序

Arrays排序

一,数组排序

      1. 数字排序  int[] intArray = new int[] { 4, 1, 3, -23 };

  Arrays.sort(intArray);

  输出: [-23, 1, 3, 4]

  2. 字符串排序,先大写后小写 String[] strArray = new String[] { "z", "a", "C" };

  Arrays.sort(strArray);

  输出: [C, a, z]

  3. 严格按字母表顺序排序,也就是忽略大小写排序 Case-insensitive sort

  Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  输出: [a, C, z]

  4. 反向排序, Reverse-order sort

  Arrays.sort(strArray, Collections.reverseOrder());

  输出:[z, a, C]

  5. 忽略大小写反向排序 Case-insensitive reverse-order sort

  Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  Collections.reverse(Arrays.asList(strArray));

  输出: [z, C, a]

二,数组和列表互转

   

         String[] str = new String[]{"a", "b", "c", "d"};
		List<String> st = Arrays.asList(str);             //数组转list
		System.out.println(str);
		
		String[] b = st.toArray(new String[st.size()]);   //list转数组
		System.out.println(b);   

  

  

Arrays排序