Sort WBS (Work Break Structure)

22 10 2007

Penulis dapat ditugaskan untuk sorting WBS (Work Break Structure) ..
wah penulis pikir logika ribet …
Penulis mencari solusi di forum dan dibantu mas Dany (senior Java).
Dia memberikan contoh 3 bagian saja seperti 2.3.1, 1,3.4, dst …
wah kurang efektif kalau jumlah bagian terbatas …

Penulis mencari solusinya kalau jumlah bagian tidak tertentu seperti :

String[] wbs = {
   "3.2.1", "1.3", "1.3.1", "1.4.11.2", "3.1.2",
   "1.3.2.1.3", "1.2", "1.1.1", "1.1.12", "2.3.4",
   "1.4.2.1", "1.1", "1.12.4","1.1.2" }; 

Arrays.sort(wbs, new Comparator() {
   public int compare(Object o1, Object o2) {
      String[] splitted = ((String) o1).split("\\.");
      String[] splitted2 = ((String) o2).split("\\.");

      int numa[] = new int[splitted.length];
      int numb[] = new int[splitted2.length];
      int totala = 0;
      int totalb = 0;
      String deca = "";
      String decb = "";
      Integer counta = 0;
      Integer countb = 0;

      int k = 0;
      for(int i = splitted.length; i > 0; i--) {
         deca = "";
	 for(int j = i-1; j > 0; j--) {
	    deca += "00";
         }
         counta = Integer.parseInt("1" + deca);
         numa[k] = Integer.parseInt(splitted[k]) * counta;
         totala += numa[k];
	 k++;
      } 				 

      int r = 0;
      for(int i = splitted2.length; i > 0; i--) {
         decb = "";
	 for(int j = i-1; j > 0; j--) {
	    decb += "00";
	 }
	 countb = Integer.parseInt("1" + decb);
	 numb[r] = Integer.parseInt(splitted2[r]) * countb;
	 totalb += numb[r];
	 r++;
      }
      return String.valueOf(totala).compareTo(String.valueOf(totalb);
   }
}); 

for (int i = 0; i < wbs.length; i++) {
   System.out.println(wbs[i]);
}

Output :

1.1
1.1.1
1.1.2
1.1.12
1.2
1.3
1.3.1
1.3.2.1.3
1.4.2.1
1.4.11.2
1.12.4
2.3.4
3.1.2
3.2.1

Akhirnya hasil sorting WBS dengan benar …🙂


Actions

Information

5 responses

26 10 2007
dhiku

Nice. Ga kepikiran caranya seperti itu. Karena memang behaviour sorting adalah mendahulukan angka dan alphabet. Sehingga 1.11 akan menjadi lebih tinggi dari 1.2.

Thx.

26 10 2007
danywhy

hahaha, mantab nich gagah, dari dulu kalo dipancing2 sama clue yg secuil malah tambah semangat, sukses yach bro ^_^

26 10 2007
danywhy

eh baru sadar gw dibilang senior wkwkwkwk, gw masih junior ach😛

26 10 2007
Martinus Ady H

Wew ampuh om🙂 wkwkwk ane malah ndak paham apa itu WBS ~_~” tahunya waktu baca2x di DW😀

18 03 2012
sony

tutorial mantap,,,
mumet tugas kuliahh,,,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: