1

I'm trying to sort data in an array that looks like this:

(String, int, String, double)

or

(David, 25, Da, 123.54)

I want the delineation for the array sort to be decided by the "integer" value. So, for example:

David, 25, Da, 123.54
Sean, 27, Pa, 514.21
Luke, 32, Ma, 221.54

These values are being read from a file, organized in a constructor,and sent back to the driver.

Here's what I have so far, I'm not sure of how much use it will be:

    public class NamesAges{
       public String[] display() throws IOException {  

    FileReader fileReader = new FileReader("elements.csv");  

    BufferedReader bufferedReader = new BufferedReader(fileReader);  
    List<String> lines = new ArrayList<String>();  
    String line = null;  

    while ((line = bufferedReader.readLine()) != null)  
    {  
        lines.add(line);  
        System.out.println(line);
    }  

    bufferedReader.close();  

    String[] sl = (String[]) lines.toArray(new String[0]);


    return sl;
    }  
}

Any tips?

1
  • "I want the delineation for the array sort to be decided by the "integer" value." O..K. Given there is exactly one column that has an integer value, how is this a 'multi-column' sort? Commented Dec 12, 2011 at 2:25

2 Answers 2

4

I'd recommend encapsulating each row into an Object and writing a Comparator to sort them as needed. Java's an object-oriented language; stop thinking in terms of primitives and Strings.

Sign up to request clarification or add additional context in comments.

3 Comments

but Strings aren't primitives.
They are the way you're using them.
True. Your wording is a bit confusing, though.
2

The easiest way would probably be to create a class that encapsulates the four values, make the class implement Comparable and define its compareTo() method via the integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.