Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#Sorting' - 6 code snippet(s) found

 Sample 1. Sorting an arraylist using Collections.sort

List<String> myList = new ArrayList<String>();
myList.add("D");
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("E");

System.out.println(myList); // Unsorted

Collections.sort(myList);

System.out.println(myList); // Sorted

   Like      Feedback     list  arraylist  collections.sort  collections


 Sample 2. Sort a List using Collections.sort and comparator

Collections.sort(listClassInfo,new Comparator<ClassInfoBean>(){
public int compare(ClassInfoBean s1,ClassInfoBean s2){
if(s1.name.compareTo(s2.name) < 0){
return -1;
} else {
return 1;
}

}});

   Like      Feedback     sorting   sort   Collections.sort   sort a list   comparator   compare method  java.util.Collections


 Sample 3. Sorting elements of a set using TreeSet

Set<String> mySet = new HashSet<String>();
mySet.add("D");
mySet.add("A");
mySet.add("B");
mySet.add("C");
mySet.add("E");

System.out.println(mySet); // May be Sorted but sorting is not guaranteed

mySet = new TreeSet<String>(mySet);

System.out.println(mySet); // Sorted

   Like      Feedback     sorting  collections  treeset  set  hashset


 Sample 4. Order Collection using google.common.collect.Ordering

Collection<Employee> sortedEmployeess = Ordering.from(new Comparator<Employee>() {
@Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getAge() - employee2.getAge();
}
};).sortedCopy(employees);

   Like      Feedback     collections  sorting collections   sorting collections using google library  comparator  compare method  google.common.collect.Ordering  google guava


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Write a Program for selection sort.

public class SelectionSort {
   public static void main(String[] args){
      int a[] = {1,3,4,5,7,8,2,6};
      
      for(int i=0;i<a.length;i++){
         for(int j=0;j<i;j++){
            if(a[j] > a[j]){
               int temporary = a[j];
               a[j] = a[i];
               a[i] = temporary;
            }
         }
      }
      
      for(int i=0;i<a.length;i++){
         System.out.println(a[i]);
      }
   }
}

   Like      Feedback     sorting algorithm  selection sort


 Sample 6. Write a program for bubble sort.

public class BubbleSort {
   public static void main(String[] args){
      int a[] = {1,3,4,5,7,8,2,6};
      
      for(int i=0;i<a.length-1;i++){
         for(int j=i;j<a.length;j++){
            if(a[j] < a[j]){
               int temporary = a[j];
               a[j] = a[i];
               a[i] = temporary;
            }
         }
      }
      
      for(int i=0;i<a.length;i++){
         System.out.println(a[i]);
      }
   }
}

   Like      Feedback     sorting algorithm  Bubble sort



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner