How to sort String array according to users input
I tried to sort String array using users input of which column they want to edit. However, sorted array cannot be displayed. I do not know how to modify or where to modify. Here, String array is made from first name, last name, and middle initial. So, logic is comparing adjacent column and of former comes later alphabetically, there is a swap going on. If two array have the same string, then compare the next row and the same column and again a swap going on. ( this is only for the 1st and 2nd column)
And also I want to modify array according to user’s input of column they want to sort. Am I correctly using Scanner class?
Here’s my code:
import java.util.Scanner;
public class Multi_dimensional_array
public static void main(String args)
// TODO Auto-generated method stub
String students= "Prachaya" , "Johnson" , "M", "Hafsa" , "Pillips", "R" , "Junnosuke", "Andews", "D", "Aithima" , "Peters", "H", "Jenwiji", "Davis","J" ;
int arrlen = students.length;
System.out.println("original array is:" );
System.out.println();
for(int count1 = 0; count1 < arrlen; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
String temp = "";
//This is sorted array according to user's input
int x = 0; int y = 0;
int column;
Scanner sc =new Scanner (System.in);
//Ask user's imput of column he/she wants to edit
System.out.println();
System.out.println("Please Enter the number of column you want to edit: ");
column = sc.nextInt();
if ( column == 1)
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 2)
y = 1;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 3)
y = 2;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
System.out.println();
System.out.println("modified array is:");
System.out.println();
for(int count1 = 0; count1 < arrlen -1; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
java arrays sorting columnsorting
add a comment |
I tried to sort String array using users input of which column they want to edit. However, sorted array cannot be displayed. I do not know how to modify or where to modify. Here, String array is made from first name, last name, and middle initial. So, logic is comparing adjacent column and of former comes later alphabetically, there is a swap going on. If two array have the same string, then compare the next row and the same column and again a swap going on. ( this is only for the 1st and 2nd column)
And also I want to modify array according to user’s input of column they want to sort. Am I correctly using Scanner class?
Here’s my code:
import java.util.Scanner;
public class Multi_dimensional_array
public static void main(String args)
// TODO Auto-generated method stub
String students= "Prachaya" , "Johnson" , "M", "Hafsa" , "Pillips", "R" , "Junnosuke", "Andews", "D", "Aithima" , "Peters", "H", "Jenwiji", "Davis","J" ;
int arrlen = students.length;
System.out.println("original array is:" );
System.out.println();
for(int count1 = 0; count1 < arrlen; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
String temp = "";
//This is sorted array according to user's input
int x = 0; int y = 0;
int column;
Scanner sc =new Scanner (System.in);
//Ask user's imput of column he/she wants to edit
System.out.println();
System.out.println("Please Enter the number of column you want to edit: ");
column = sc.nextInt();
if ( column == 1)
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 2)
y = 1;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 3)
y = 2;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
System.out.println();
System.out.println("modified array is:");
System.out.println();
for(int count1 = 0; count1 < arrlen -1; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
java arrays sorting columnsorting
2
Usin a 2D array is a horrible way of doing that. Create a Student class, and use a Student array (i.e.Student
. Then google for " how to sort objects in Java".
– JB Nizet
Nov 11 at 8:55
3
Anyway, all you need isArrays.sort(students, Comparator.comparing(student -> student[colomb]))
. Note that the proper term iscolumn
, notcolomb
.
– JB Nizet
Nov 11 at 9:00
2
this code is almost unreadable. You should learn to reuse code by using separate methods for code used in several places ...
– AKSW
Nov 11 at 9:25
add a comment |
I tried to sort String array using users input of which column they want to edit. However, sorted array cannot be displayed. I do not know how to modify or where to modify. Here, String array is made from first name, last name, and middle initial. So, logic is comparing adjacent column and of former comes later alphabetically, there is a swap going on. If two array have the same string, then compare the next row and the same column and again a swap going on. ( this is only for the 1st and 2nd column)
And also I want to modify array according to user’s input of column they want to sort. Am I correctly using Scanner class?
Here’s my code:
import java.util.Scanner;
public class Multi_dimensional_array
public static void main(String args)
// TODO Auto-generated method stub
String students= "Prachaya" , "Johnson" , "M", "Hafsa" , "Pillips", "R" , "Junnosuke", "Andews", "D", "Aithima" , "Peters", "H", "Jenwiji", "Davis","J" ;
int arrlen = students.length;
System.out.println("original array is:" );
System.out.println();
for(int count1 = 0; count1 < arrlen; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
String temp = "";
//This is sorted array according to user's input
int x = 0; int y = 0;
int column;
Scanner sc =new Scanner (System.in);
//Ask user's imput of column he/she wants to edit
System.out.println();
System.out.println("Please Enter the number of column you want to edit: ");
column = sc.nextInt();
if ( column == 1)
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 2)
y = 1;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 3)
y = 2;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
System.out.println();
System.out.println("modified array is:");
System.out.println();
for(int count1 = 0; count1 < arrlen -1; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
java arrays sorting columnsorting
I tried to sort String array using users input of which column they want to edit. However, sorted array cannot be displayed. I do not know how to modify or where to modify. Here, String array is made from first name, last name, and middle initial. So, logic is comparing adjacent column and of former comes later alphabetically, there is a swap going on. If two array have the same string, then compare the next row and the same column and again a swap going on. ( this is only for the 1st and 2nd column)
And also I want to modify array according to user’s input of column they want to sort. Am I correctly using Scanner class?
Here’s my code:
import java.util.Scanner;
public class Multi_dimensional_array
public static void main(String args)
// TODO Auto-generated method stub
String students= "Prachaya" , "Johnson" , "M", "Hafsa" , "Pillips", "R" , "Junnosuke", "Andews", "D", "Aithima" , "Peters", "H", "Jenwiji", "Davis","J" ;
int arrlen = students.length;
System.out.println("original array is:" );
System.out.println();
for(int count1 = 0; count1 < arrlen; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
String temp = "";
//This is sorted array according to user's input
int x = 0; int y = 0;
int column;
Scanner sc =new Scanner (System.in);
//Ask user's imput of column he/she wants to edit
System.out.println();
System.out.println("Please Enter the number of column you want to edit: ");
column = sc.nextInt();
if ( column == 1)
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 0;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 2)
y = 1;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 1;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
if ( column == 3)
y = 2;
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
//swap array[0][0] and array [1][0] / [1][0] and [2][0] / [2][0] and [3][0] / [3][0] and [4][0] / [4][0] and [5][0]
if (students[x][y].compareTo(students[x + 1][y])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
//compare the top array with bottom strings
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
else
if( x < arrlen - 1)
for (x = 0; x < arrlen; x++)
if (students[x][y].compareTo(students[x + 1][y]) == 0)
if (students[x][y + 1].compareTo(students[x + 1][y + 1])> 0)
temp = students[x][y] ;
students[x][y] = students[x + 1][y];
students[x + 1][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + 1][y + 1];
students[x + 1][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + 1][y + 2];
students[x + 1][y + 2] = temp;
x = 0; y = 2;
for (int j = x + 2; j< arrlen; j++)
if (students[x][y].compareTo(students[x + 2][y])> 0 )
temp = students[x][y] ;
students[x][y] = students[x + j][y];
students[x + j][y] = temp;
temp = students[x][y + 1] ;
students[x][y + 1] = students[x + j][y + 1];
students[x + j][y + 1] = temp;
temp = students[x][y + 2] ;
students[x][y + 2] = students[x + j][y + 2];
students[x + j][y + 2] = temp;
System.out.println();
System.out.println("modified array is:");
System.out.println();
for(int count1 = 0; count1 < arrlen -1; count1++)
for(int count2 = 0; count2 < arrlen -2; count2++)
System.out.print(students[count1][count2] + " t" );
System.out.println();
java arrays sorting columnsorting
java arrays sorting columnsorting
edited Nov 11 at 12:54
Joakim Danielson
6,6153623
6,6153623
asked Nov 11 at 8:49
Junnosuke Kamohara
42
42
2
Usin a 2D array is a horrible way of doing that. Create a Student class, and use a Student array (i.e.Student
. Then google for " how to sort objects in Java".
– JB Nizet
Nov 11 at 8:55
3
Anyway, all you need isArrays.sort(students, Comparator.comparing(student -> student[colomb]))
. Note that the proper term iscolumn
, notcolomb
.
– JB Nizet
Nov 11 at 9:00
2
this code is almost unreadable. You should learn to reuse code by using separate methods for code used in several places ...
– AKSW
Nov 11 at 9:25
add a comment |
2
Usin a 2D array is a horrible way of doing that. Create a Student class, and use a Student array (i.e.Student
. Then google for " how to sort objects in Java".
– JB Nizet
Nov 11 at 8:55
3
Anyway, all you need isArrays.sort(students, Comparator.comparing(student -> student[colomb]))
. Note that the proper term iscolumn
, notcolomb
.
– JB Nizet
Nov 11 at 9:00
2
this code is almost unreadable. You should learn to reuse code by using separate methods for code used in several places ...
– AKSW
Nov 11 at 9:25
2
2
Usin a 2D array is a horrible way of doing that. Create a Student class, and use a Student array (i.e.
Student
. Then google for " how to sort objects in Java".– JB Nizet
Nov 11 at 8:55
Usin a 2D array is a horrible way of doing that. Create a Student class, and use a Student array (i.e.
Student
. Then google for " how to sort objects in Java".– JB Nizet
Nov 11 at 8:55
3
3
Anyway, all you need is
Arrays.sort(students, Comparator.comparing(student -> student[colomb]))
. Note that the proper term is column
, not colomb
.– JB Nizet
Nov 11 at 9:00
Anyway, all you need is
Arrays.sort(students, Comparator.comparing(student -> student[colomb]))
. Note that the proper term is column
, not colomb
.– JB Nizet
Nov 11 at 9:00
2
2
this code is almost unreadable. You should learn to reuse code by using separate methods for code used in several places ...
– AKSW
Nov 11 at 9:25
this code is almost unreadable. You should learn to reuse code by using separate methods for code used in several places ...
– AKSW
Nov 11 at 9:25
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247144%2fhow-to-sort-string-array-according-to-users-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247144%2fhow-to-sort-string-array-according-to-users-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Usin a 2D array is a horrible way of doing that. Create a Student class, and use a Student array (i.e.
Student
. Then google for " how to sort objects in Java".– JB Nizet
Nov 11 at 8:55
3
Anyway, all you need is
Arrays.sort(students, Comparator.comparing(student -> student[colomb]))
. Note that the proper term iscolumn
, notcolomb
.– JB Nizet
Nov 11 at 9:00
2
this code is almost unreadable. You should learn to reuse code by using separate methods for code used in several places ...
– AKSW
Nov 11 at 9:25