Sorted…sorted…java

setelah bolak-balik  ke jogja, saatnya update blog…

simple sort:


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortedSimple {
 public static void main(String[] args){
 List<String> mo = new ArrayList<String>();
 mo.add("znwar");
 mo.add("codi");
 mo.add("doni");
 mo.add("ahmad");
 mo.add("reza");
 mo.add("slamet");
 System.out.println(mo);
 Collections.sort(mo);
 System.out.println(mo);
 }
}



sort with input



public class CompareList implements Comparable<CompareList> {
 private String judul;

 CompareList(String judule) {
 this.judul = judule;
 }
 public String getJudul(){
 return judul;
 }

 @Override
 public String toString(){
 return judul + " ";
 }

 public int compareTo(CompareList o) {
 return judul.compareTo(o.getJudul());
 }

}

&nbsp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Compare {
 public static void main(String[] args) throws IOException{
 Scanner input = new Scanner(System.in);
 List<CompareList> o = new ArrayList<CompareList>();

 System.out.println("Masukkan jmlh data ");
 int jml = input.nextInt();
 int[] jmla = new int[jml];
 String[] judul = new String[jml];
 String judule,genre;
 for(int i=0;i<jmla.length;i++){
 System.out.print("Masukkan judul :");
 judule = input.next();
 o.add(new CompareList(judule));
 Collections.sort(o);
 }

 for(CompareList t: o){
 System.out.print(t);
 }

 }
}

semoga bermanfaat..

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