aboutsummaryrefslogtreecommitdiff
path: root/challenge-241/deadmarshal/java/Ch2.java
blob: e810fe142905d2abd5175f97d9c3141cf4e9167d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;

public class Ch2 {
  public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<>(List.of(11,8,27,4));
    prime_order(list);
    System.out.println(list);
  }

  private static int count_factors(int n) {
    int c = 2,count = 0;
    while(n > 1) {
      if(n % c == 0) {
	n /= c;
	count++;
      }
      else c++;
    }
    return count;
  }

  private static void prime_order(List<Integer> list) {
    list.sort(new Comparator<Integer>() {
      @Override
      public int compare(Integer a, Integer b) {
      	int fa = count_factors(a);
      	int fb = count_factors(b);
      	return fa == fb ? Integer.compare(a,b) :
	  Integer.compare(fa,fb);
      }});
  }
}