๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜

9๊ฐ•. Java์—์„œ์˜ ์ •๋ ฌ

by devshin.kr 2020. 11. 5.
728x90
๋ฐ˜์‘ํ˜•

<๊ธฐ๋ณธ ํƒ€์ž… ๋ฐ์ดํ„ฐ ์ •๋ ฌ>

- Arrays  ํด๋ž˜์Šค๊ฐ€ primitive type์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์œ„ํ•œ ์ •๋ ฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

int[] data = new int[capacity];

// data[0]์—์„œ data[capacity-1]๊นŒ์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฝ‰ ์ฐจ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๋ ฌํ•œ๋‹ค.
Arrays.sort(data);

//๋ฐฐ์—ด์ด ๊ฝ‰ ์ฐจ์žˆ์ง€ ์•Š๊ณ , data[0]์—์„œ data[size-1]๊นŒ์ง€ size๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋งŒ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๋ ฌํ•œ๋‹ค.
Arrays.sort(data, 0, size);

 

- int ์ด์™ธ์˜ ๋‹ค๋ฅธ primitive type ๋ฐ์ดํ„ฐ(double, char ๋“ฑ..)์— ๋Œ€ํ•ด์„œ๋„ ์ œ๊ณตํ•œ๋‹ค.

 

 

<๊ฐ์ฒด์˜ ์ •๋ ฌ : ๋ฌธ์ž์—ด>

Primitive type ๋ฐ์ดํ„ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Arrays.sort() ๋ฉ”์„œ๋“œ๋กœ ์ •๋ ฌ๋œ๋‹ค.

// fruits๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฐฐ์—ด ์„ ์–ธ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”
String[] fruits = new String[] {"pineapple", "apple", "orange", "banana"};

// fruits๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฐฐ์—ด ์ •๋ ฌ
Arrays.sort(fruits);

// ์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์™€ ์ถœ๋ ฅ
for(String name : fruits)
	System.out.println(name);

 

 

<ArrayList ์ •๋ ฌ : ๋ฌธ์ž์—ด>

Collections.sort() ๋ฉ”์„œ๋“œ๋กœ ์ •๋ ฌ๋œ๋‹ค.

// fruits๋ผ๋Š” ์ด๋ฆ„์˜ ArrayList ์„ ์–ธ ํ›„ add๋กœ ๊ฐ’์„ ์ถ”๊ฐ€
List<String> fruits = new ArrayList<String>();
fruits.add("pineapple");
fruits.add("apple");
fruits.add("orange");
fruits.add("banana");

// ์ •๋ ฌ
Collections.sort(fruits);

// ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
for(String name : fruits)
	System.out.println(name);

 

 

<๊ฐ์ฒด์˜ ์ •๋ ฌ : ์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด>

public class Fruit{
	public String name;
    public int quantity;
    public Furit(String name, int quantity){
    	this.name = name;
        this.quantity = quantity;
    }
}

// somewhere in your program.. ๋‹น์‹ ์˜ ํ”„๋กœ๊ทธ๋žจ ์–ด๋”˜๊ฐ€์— ใ…‹
Fruit[] fruits = new Fruit[4];
fruits[0] = new Fruit("pineapple", 70);
fruits[1] = new Fruit("apple", 100);
fruits[2] = new Fruit("orange", 80);
fruits[3] = new Fruit("banana", 90);

Arrays.sort(fruits);

=> ์ด ๊ฒฐ๊ณผ๋Š” name์œผ๋กœ ์ •๋ ฌ์ด ๋  ์ง€, quantity๋กœ ์ •๋ ฌ์ด ๋  ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค....

 

 

<๊ฐ์ฒด์˜ ์ •๋ ฌ : Comparable Interface 1>

public class Fruit implements Comparable<Fruit>{
	public String name;
    public int quantity;
    public Furit(String name, int quantity){
    	this.name = name;
        this.quantity = quantity;
    }
    
    public int compareTo(Fruit other){
    	// ๋‚ด ์ด๋ฆ„๊ณผ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ๋น„๊ตํ•˜๊ณ  ์ž‘์€ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
    	return name.compareTo(other.name);
    }
}

// somewhere in your program
Fruit[] fruits = new Fruit[4];
fruits[0] = new Fruit("pineapple", 70);
fruits[1] = new Fruit("apple", 100);
fruits[2] = new Fruit("orange", 80);
fruits[3] = new Fruit("banana", 90);

Arrays.sort(fruits);

 

 

<๊ฐ์ฒด์˜ ์ •๋ ฌ : Comparable Interface 2>

public class Fruit implements Comparable<Fruit>{
	public String name;
    public int quantity;
    public Furit(String name, int quantity){
    	this.name = name;
        this.quantity = quantity;
    }
    
    public int compareTo(Fruit other){
    	// return value is ์Œ์ˆ˜ or 0 or ์–‘์ˆ˜
        // ์Œ์ˆ˜ : ๋‚ด๊ฐ€ ๋น„๊ต๋Œ€์ƒ๋ณด๋‹ค ์ž‘๋‹ค.
        // 0 : ๋‚˜์™€ ๋น„๊ต๋Œ€์ƒ์˜ ๊ฐ’์ด ๋™์ผํ•˜๋‹ค.
        // ์–‘์ˆ˜ : ๋‚ด๊ฐ€ ๋น„๊ต๋Œ€์ƒ๋ณด๋‹ค ํฌ๋‹ค.
    	return quantity - other.quantity;
    }
}

// somewhere in your program
Fruit[] fruits = new Fruit[4];
fruits[0] = new Fruit("pineapple", 70);
fruits[1] = new Fruit("apple", 100);
fruits[2] = new Fruit("orange", 80);
fruits[3] = new Fruit("banana", 90);

Arrays.sort(fruits);

 

 

<๋‘ ๊ฐ€์ง€ ๊ธฐ์ค€์„ ๋™์‹œ์— ์ง€์›ํ•˜๋ ค๋ฉด?>

- ํ•˜๋‚˜์˜ ๊ฐ์ฒด ํƒ€์ž…์— ๋Œ€ํ•ด์„œ 2๊ฐ€์ง€ ์ด์ƒ์˜ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ์ง€์›ํ•˜๋ ค๋ฉด, Comparator๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

// Comparator ํด๋ž˜์Šค๋ฅผ extendsํ•˜๋ฉฐ, compare ๋ฉ”์„œ๋“œ๋ฅผ overridingํ•˜๋Š” ์ƒˆ๋กœ์šด ์ด๋ฆ„ ์—†๋Š” ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ ํ›„, ๊ทธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•œ๋‹ค.

Comparator<Fruit> nameComparator = new Comparator<Fruit>() {
	public int compare (Fruit fruit1, Fruit fruit2) {
    	return fruit1.name.compareTo(fruit2.name);
    }
};

Comparator<Fruit> quantComparator = new Comparator<Fruit>() {
	public int compare(Fruit fruit1, Fruit fruit2) {
    	return fruit1.quantity - fruit2.quantity;
    }
};

Arrays.sort(fruits, nameComparator);
// ๋˜๋Š”
Arrays.sort(fruits, quantComparator);

 

 

<Comparator>

public class Fruit{
      public String name;
      public int quantity;
      public Fruit(String name, int quantity) {
          this.name = name;
          this.quantity = quantity;
      }

    public static Comparator<Fruit> nameComparator = new Comparator<Fruit>() {
        public int compare (Fruit fruit1, Fruit fruit2) {
            return fruit1.name.compareTo(fruit2.name);
        }
    };

    public static Comparator<Fruit> quantComparator = new Comparator<Fruit>() {
        public int compare(Fruit fruit1, Fruit fruit2) {
            return fruit1.quantity - fruit2.quantity;
        }
    };
  
}


// main์—์„œ ์ •๋ ฌํ•  ๋•Œ์—๋Š”
// Arrays.sort(fruits, Fruit.nameComparator);
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€