-3
I am sorting numbers from a CSV file. I import the numbers and step into an Array List and convert the ArrayList<String>
for ArrayList<Integer>
. It turns out I’m having a little logic problem sorting the numbers. The numbers have even organized, but the largest number is coming out in front of everyone, and only from the next number that starts the ascending order, as below:
Method to pass file numbers . CSV to Arraylist:
public void PassVector()
{
String LineFile = new String ();
File fileCSV = new File(LocalFile.getText());
try
{
Numb = (ArrayList<String>) Files.lines(Paths.get(LocalFile.getText()))
.flatMap(Pattern.compile(",")::splitAsStream)
.collect(Collectors.toList());
System.out.println(Numb);
Scanner reader = new Scanner(fileCSV);
while (reader.hasNext())
{
LineFile = reader.nextLine();
NumbLines+=1;
}
NumbNumbers = NumbLines * 2;
//System.out.println(NumbLines);
//ConvertArrayList(Numb);
}
catch (IOException e)
{
System.out.println("Não foi possível carregar o vetor. \n\n Código do erro: " + e);
}
}
Method to convert the ArrayList <String>
for ArrayList<Integer>
public static ArrayList<Integer> getIntegerArray(ArrayList<String> ArrayConvert) {
ArrayList<Integer> result = new ArrayList<Integer>();
for(String stringValue : ArrayConvert) {
try {
//Convert String to Integer, and store it into integer array list.
result.add(Integer.parseInt(stringValue));
} catch(NumberFormatException nfe) {
//System.out.println("Could not parse " + nfe);
//Log.w("NumberFormat", "Parsing failed! " + stringValue + " can not be an integer");
}
}
return result;
}
Bubblesort Sorting Method (With logical error):
public static void Bubble(ArrayList<Integer> BubbleVector)
{
for (int i=0; i<BubbleVector.size();i=i+2)
{
for (int j=2; j<(BubbleVector.size()-1);j=j+2)
{
if (BubbleVector.get(i) < BubbleVector.get(j))
{
Auxiliar1 = BubbleVector.get(i);
Auxiliar2 = BubbleVector.get(i+1);
BubbleVector.set(i, BubbleVector.get(j));
BubbleVector.set(i+1, BubbleVector.get(j+1));
BubbleVector.set(j, Auxiliar1);
BubbleVector.set(j+1, Auxiliar2);
}
}
}
System.out.println(BubbleVector);
}
CSV file with numbers to be ordered
Collections.sort(BubbleVector)
Simple as that.– user28595