array sort

[Java] Collection sort

2016. 1. 7. 17:47
반응형
  • JAVA Sort, ArraySort
import java.util.ArrayList;
import java.util.Collections;

public class MainClass{
  public static void main(String[] args) {
    
    String[] stringArray = new String[]{"c","e","a","k"};
    List<string> list = Arrays.asList(stringArray );
    //ArrayList<string> list = new ArrayList<string>();
    
    for(String s: stringArray){
      list.add(s);
    }

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    
    for (String s: list) {
      System.out.println(s);
    }
  }
}


public class ArraySort {
    public static void main(String[] args) {
 
        String[] s = { "b", "A", "D", "C", "a", "F" };
 
        // 대소문자 구분 정렬
        Arrays.sort(s);
        System.out.println("==&gt; 대소문자 구분");
        System.out.println(Arrays.toString(s)); //[A, C, D, F, a, b] 
 
        // 역순정렬
        descArraySort(s);
        System.out.println("==&gt; 역순정렬 ");
        System.out.println(Arrays.toString(s)); //[b, a, F, D, C, A] 
 
        // 대소문자 구분 없이 정렬
        System.out.println("==&gt; 대소문자 구분 없이 ");  
        Arrays.sort(s, String.CASE_INSENSITIVE_ORDER);
        System.out.println(Arrays.toString(s)); //[a, A, b, C, D, F]
 
    }
 
    public static void descArraySort(String[] arr) {
        String temp;
 
        for (int i = 0; i &lt; arr.length / 2; i++) {
            temp = arr[i];
            arr[i] = arr[(arr.length - 1) - i];
            arr[(arr.length - 1) - i] = temp;
        }
    }
}

 

반응형

+ Recent posts

반응형