My problem is the following:
I have a .csv file in which the attributes of an object are stored, which are: CP, Street, number, neighborhood, recipient I want to order these attributes based on the CP with the following code:
public class ExternalSort{
static int N = 2000000; /** size of the file in disk */
static int M = 100000; /** max items the memory buffer can hold */
public static void externalSort(String fileName)
{
String tfile = "temp-file-";
int[] buffer = new int[M < N ? M : N];
try
{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
int slices = (int) Math.ceil((double) N/M);
int i, j;
i = j = 0;
/** Iterar a través de los elementos del archivo */
for (i = 0; i < slices; i++)
{
/** Leer fragmentos de M-elementos a la vez desde el archivo */
for (j = 0; j < (M < N ? M : N); j++)
{
String t = br.readLine();
if (t != null)
buffer[j] = Integer.parseInt(t.substring(0,4));
else
break;
}
/** Sort M elementos */
Arrays.sort(buffer);
/** Escriba los números ordenados en el archivo temporal */
FileWriter fw = new FileWriter(tfile + Integer.toString(i) + ".txt");
PrintWriter pw = new PrintWriter(fw);
for (int k = 0; k < j; k++)
pw.println(buffer[k]);
pw.close();
fw.close();
}
br.close();
fr.close();
/** Ahora abra cada archivo y combinelos, luego vuelva a escribirlos en el disco */
int[] topNums = new int[slices];
BufferedReader[] brs = new BufferedReader[slices];
for (i = 0; i < slices; i++)
{
brs[i] = new BufferedReader(new FileReader(tfile + Integer.toString(i) + ".txt"));
String t = brs[i].readLine();
if (t != null)
topNums[i] = Integer.parseInt(t);
else
topNums[i] = Integer.MAX_VALUE;
}
FileWriter fw = new FileWriter("external-sorted.txt");
PrintWriter pw = new PrintWriter(fw);
for (i = 0; i < N; i++)
{
int min = topNums[0];
int minFile = 0;
for (j = 0; j < slices; j++)
{
if (min > topNums[j])
{
min = topNums[j];
minFile = j;
}
}
pw.println(min);
String t = brs[minFile].readLine();
if (t != null)
topNums[minFile] = Integer.parseInt(t);
else
topNums[minFile] = Integer.MAX_VALUE;
}
for (i = 0; i < slices; i++)
brs[i].close();
pw.close();
fw.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(NullPointerException ex){
ex.printStackTrace();
}
}
static String generateInput(int n)
{
String fileName = "external-sort.txt";
Random rand = new Random();
try
{
FileWriter fw = new FileWriter(fileName);
PrintWriter pw = new PrintWriter(fw);
for (int i = 0; i < n; i++)
pw.println(rand.nextInt(9900) + 100);
pw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return fileName;
}
It is supposed to return the file ordered based on the CP but it doesn't.
As an example I have the following data:
68148, Sur, 45, Real Casa Blanca (Residential), Juan Gabriel Contreras Caceres
68010, Sur, 57, Elsa (Subdivision), Tomas Prieto Jurado
68045, East, 144, Star (Cologne), Jorge Conde Prieto
And the ordered file should be like this:
68010, Sur, 57, Elsa (Subdivision), Tomas Prieto Jurado
68045, East, 144, Star (Cologne), Jorge Conde Prieto
68148, Sur, 45, Real Casa Blanca (Residential), Juan Gabriel Contreras Caceres
If you want to order an object of a class created by you, you can do it like this
implement the Comparable Interface and then implement its abstract method compareTo
import java.util.*;
public class StackOverflow implements Comparable{
}
I hope it works for you. Regards.