Trying to sort elements after reading from a file

2306 views file
4

So essentially I'm trying to read a line of strings from a file and sort them based on one of the strings.

File data:

BROKER,ADD,XYZ98765,Jose,Ferguson,321-111,0.02
BROKER,ADD,LOT20483,Kerri,Andrews,311-862,0.04
BROKER,ADD,ABC12345,Bill,Paxton,123-456,0.10
BROKER,ADD,AFI90037,Adam,Jones,331-715,0.12
BROKER,ADD,KFH58473,Susan,Riddle,313-225,0.08
BROKER,ADD,DKW47219,Tyler,Cross,213-551,0.12

Here is where I read from the file:

private static void processFile() {

    try {
        File mainFile = (new File(INPUT_FILENAME));
        System.out.println("Reading data from file " + INPUT_FILENAME);
        Scanner readFile = new Scanner(mainFile);
        while (readFile.hasNextLine()) {
            String nextLine = readFile.nextLine();
            String[] values = nextLine.split(",");
            if ("BROKER".equals(values[0])) {
            if ("ADD".equals(values[1])) {
                processFundManagerAddition(values);
            }
        }
    }
    catch (IOException e) {
    ...
    }

And here is where I create the output:

public static void processFundManagerAddition(String[] values) {

    FundManager newFm = new FundManager(values);

    if (!FundManagerLogImpl.isLicenseUnique(values[2])) {
        FundManagerLogImpl.addFundManager(newFm);
        System.out.println("  ADDED: FundManager with license " + values[2]);
    }
}

The output is:

Reading data from file input/assn1input1.txt
    ADDED: FundManager with license XYZ98765
    ADDED: FundManager with license LOT20483
    ADDED: FundManager with license ABC12345
    ADDED: FundManager with license AFI90037
    ADDED: FundManager with license KFH58473
    ADDED: FundManager with license DKW47219

So what I'm wondering is how can I sort the license numbers (values[2]) in alphabetical order, so that when the list prints it's formatted as such:

Reading data from file input/assn1input1.txt
    ADDED: FundManager with license ABC12345 
    ADDED: FundManager with license AFI90037 
    ADDED: FundManager with license DKW47219
    ADDED: FundManager with license KFH58473
    ADDED: FundManager with license LOT20483
    ADDED: FundManager with license XYZ98765

answered question

Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.

And please always include the language and possible framework you're using as tags. We shouldn't have to guess about that.

1 Answer

4

Assuming you use Java: To get a sorted output you either need to sort the file before reading it (do NOT do this) or to first read the whole file and sort afterwards.

For example: Create an object for each line like you do in the 'FundManager' class. Store these objects in a list. Create a Comperator for these objects. Pass the List and the Comperator into the method sort from the class Collections. This will sort the list according to the specified Comperator. Walk through the list in a for loop and print out the values.

There is a detailed description for this approach in the post: How to sort ArrayList using Comparator?

posted this

Have an answer?

JD

Please login first before posting an answer.