How to sort String array according to users input











up vote
0
down vote

favorite












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();
}

}
}









share|improve this question




















  • 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 is column, not colomb.
    – 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















up vote
0
down vote

favorite












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();
}

}
}









share|improve this question




















  • 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 is column, not colomb.
    – 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













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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();
}

}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:54









Joakim Danielson

5,6843521




5,6843521










asked Nov 11 at 8:49









Junnosuke Kamohara

22




22








  • 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 is column, not colomb.
    – 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




    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 is column, not colomb.
    – 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

















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',
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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python