Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Java program to sort a map by value.

Question:
How to sort or order a HashMap or TreeSet or any map item by value?

Answer:
Write a comparator which compares by value, not by key.

Code:
import java.util.*;
import java.util.Map.*;
 
public class SortHashMap {
 
  public static void main(String a[]){
 
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("apple", 20);
    map.put("plum", 11);
    map.put("orange", 16);
    map.put("peach", 3);
    map.put("grape", 7);
 
    Set<Entry<String,Integer>> set = map.entrySet();
    List<Entry<String,Integer>> list = new ArrayList<Entry<String,Integer>>(set);
 
    Collections.sort(list, new Comparator<Map.Entry<String,Integer>>() {
      public int compare( Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
        return (o2.getValue()).compareTo( o1.getValue());
      }
    });
 
    for(Map.Entry<String, Integer> entry:list){
      System.out.println(entry.getValue() + " " + entry.getKey());
    }
  }
}

Output:
$ java SortHashMap
20 apple
16 orange
11 plum
7 grape
3 peach

How to test if two words are anagram using Arrays.sort()?

Question:
How to test if two words are anagram using Arrays.sort()?

Code:
import java.util.*;
 
public class Anagram {
 
  public static void main(String[] args) {
 
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter first word: ");
    String word1 = sc.nextLine();
    System.out.print("Enter second word: ");
    String word2 = sc.nextLine();
 
    System.out.println("Anagram: " + isAnagram(word1,word2));
 
   }
 
 
  // Test if two words are Anagram using Arrays.sort()
  public static boolean isAnagram(String word1, String word2) {
 
    word1 = word1.toLowerCase();
    word2 = word2.toLowerCase();
 
    char[] char1 = word1.toCharArray();
    char[] char2 = word2.toCharArray();
 
    Arrays.sort(char1);
    Arrays.sort(char2);
 
    return Arrays.equals(char1, char2);
  }
}
 

Output:
$ java Anagram
Enter first word: Angel
Enter second word: Glean
Anagram: true

$ java Anagram
Enter first word: bob
Enter second word: bobby
Anagram: false

Sorting a parallel array

Question:
Need help sorting parallel array:
    Double [] salary = {500.00, 200.00, 1000.00, 2500.00};
    String [] name = {"Sam", "Dave", "Jake", "Ryan"}; 

Code:
public class Example {
  public static void main(String[] args) {
 
    // parallel array
    Double [] salary = {500.00, 200.00, 1000.00, 2500.00};
    String [] name = {"Sam", "Dave", "Jake", "Ryan"}; 
 
    System.out.println("Before:");
    for (int n = 0; n < name.length; n++) {
      System.out.println(name[n] + ":" + salary[n]);
    }
 
 
    int remaining = salary.length - 1;
      for(int x = 0; x < (salary.length-1); x++) {
         for(int y = 0; y < (remaining); y++) {
            double tmp1;
            String tmp2;
            if ( salary[y] > salary[y+1] ) {
 
              tmp1 =  salary[y+1]; 
              salary[y+1] = salary[y];
              salary[y] = tmp1;
 
              tmp2 =  name[y+1]; 
              name[y+1] = name[y];
              name[y] = tmp2;
 
            }
         }
         remaining--;
      }
 
    System.out.println("After:");
    for (int n = 0; n < name.length; n++) {
      System.out.println(name[n] + ":" + salary[n]);
    }
 
  }
}

Output:
$ java Example 
Before:
Sam:500.0
Dave:200.0
Jake:1000.0
Ryan:2500.0
After:
Dave:200.0
Sam:500.0
Jake:1000.0
Ryan:2500.0

How to sort an array of ints?

Question:
How do I do the following:

A user will input 3 different numbers and the program should return a 3-digit number in ascending order of the digits.

For example, if the user inputs 6, 1, 5 the returned integer should be 156.

Answer:
Put the three numbers in a array. Use the method Arrays.sort() to sort them. Then a little math to get to the final number.

Code:
import java.util.*;
 
public class SortNumbers {
 
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      int[] input = new int[3];
 
      System.out.print("Enter Number: ");
      input[0] = sc.nextInt();
 
      System.out.print("Enter Number: ");
      input[1] = sc.nextInt();
 
      System.out.print("Enter Number: ");
      input[2] = sc.nextInt();
 
      System.out.println("Before: " + Arrays.toString(input));
      Arrays.sort(input);
      System.out.println("After: " + Arrays.toString(input));
 
      int number = input[0]*100;
      number = number + (input[1]*10);
      number = number + input[2];
 
      System.out.println("Number: " + number);
   }
}

Output:
$ java SortNumbers 
Enter Number: 6
Enter Number: 1
Enter Number: 5
Before: [6, 1, 5]
After: [1, 5, 6]
Number: 156