fork download
  1. import java.io.*;
  2. public class Main {
  3.  
  4. static String[][] criminals = new String[10][3]; // create 3 columns, 10 rows
  5.  
  6. public static void main(String[] args)throws IOException {
  7. int i, j;
  8. int smallest; //smallest is the current smallest element
  9. String[] temp; //make an element swap
  10.  
  11. for (i = 0; i < criminals.length ; i++) {
  12. criminals[i][0] = br.readLine(); //Criminal Name
  13. criminals[i][1] = br.readLine(); //Criminal Crime
  14. criminals[i][2] = br.readLine(); //Year of conviction
  15. }
  16.  
  17. System.out.println("******Unsorted list******");
  18. for (i = 0; i < criminals.length; i++) {
  19. System.out.println(criminals[i][0] + " - " + criminals[i][1] + " - " + criminals [i][2]);
  20. }
  21.  
  22. System.out.println("******Sorted list (by crime)******");
  23.  
  24. for(i = 0; i < 10; i++){
  25. smallest = i;
  26.  
  27. for(j = i+1; j < 10; j++){
  28. if(criminals[smallest][1].compareTo(criminals[j][1]) > 0){
  29. smallest = j;
  30. }
  31. }
  32.  
  33. temp = criminals[i];
  34. criminals[i] = criminals[smallest];
  35. criminals[smallest] = temp;
  36. }
  37.  
  38. for(i = 0; i < 10; i++){
  39. System.out.println(criminals[i][0] + " - " + criminals[i][1] + " - " + criminals [i][2]);
  40. }
  41. System.out.println("The list has been sorted.");
  42. }
  43. }
  44.  
Success #stdin #stdout 0.08s 380160KB
stdin
Al Capone
arson
2009
Slippery Sal
theft
2001
Nada
arson
1987
Slippery Sal
theft
1999
Salma
assault
2010
Scooby Doo
theft
1998
Velma
assault
1991
Daphne
arson
1976
Fred
assault
2003
Shaggy
arson
2007
stdout
******Unsorted list******
Al Capone - arson - 2009
Slippery Sal - theft - 2001
Nada - arson - 1987
Slippery Sal - theft - 1999
Salma - assault - 2010
Scooby Doo - theft - 1998
Velma - assault - 1991
Daphne - arson - 1976
Fred - assault - 2003
Shaggy - arson - 2007
******Sorted list (by crime)******
Al Capone - arson - 2009
Nada - arson - 1987
Daphne - arson - 1976
Shaggy - arson - 2007
Salma - assault - 2010
Velma - assault - 1991
Fred - assault - 2003
Slippery Sal - theft - 2001
Scooby Doo - theft - 1998
Slippery Sal - theft - 1999
The list has been sorted.