0

I'm trying to write a code that reads a Contacts list from a .txt file, sorts it by name and outputs it (sortByNames.txt) Then sorts it by their phone numbers and outputs a different new file (sortByNumbers.txt)

I.E:

  Michael - 0001234567
  Robert  - 0019234568
  Jacob   - 0011234567

needs to become this by number:

  Michael - 0001234567
  Jacob   - 0011234567
  Robert  - 0019234568

and this by name:

  Jacob   - 0011234567
  Michael - 0001234567
  Robert  - 0019234568

Here's what I have so far:

public static void Save(String[][] vals, File f) throws Exception {
    PrintWriter output = new PrintWriter(f);

    for (int rows = 0; rows < vals.length; rows++) {
        for (int cols = 0; cols < vals[rows].length; cols++) {
            output.print(vals[rows][cols]);
            output.print(" ");
        }
        output.println();
    }

    output.flush();
    output.close();
    return;
}

public static int CountLines(File f, Scanner reader) throws Exception {
    int lines = 0;
    while (reader.hasNextLine()) {
        String s = reader.nextLine();
        s = s.trim();
        if (s.length() == 0) {
            break;
        }
        lines++;
    }
    return lines;
}

public static void main(String[] args) throws Exception {
    File contacts = new File("Contacts.txt");
    Scanner reader = new Scanner(contacts);
    String[][] s = new String[CountLines(contacts, reader)][2];


    File name = new File("SortedByName.txt");

    File number = new File("SortedByNumber.txt");
}

As you can see, I've figured out how many contacts there are and how to save the data into a .txt file.

HOWEVER, I can't figure out how to sort it by either their numbers or even their names.

Can someone please help me out with this? I've been racking my brain for the past couple hours and I've tried searching it up (But it leads me to code I can't understand).

How do I sort by Names? How do I sort by their numbers?

Thank you so much for any and all help!!

2 Answers 2

1

Simple with Java 8:

Arrays.sort(s, (a, b) -> a[0].compareTo(b[0])); //sort by name
Arrays.sort(s, (a, b) -> a[1].compareTo(b[1])); //sort by number

Hope it can help you out. Edit: You can refer this code. You can comment out Sort with java 8 to test another way

public static void main(String... strings) {

    //Here we sort by number at 0 position
    String[][] s = new String[2][2];
    s[0][0] = "2";
    s[0][1] = "a";
    s[1][0] = "1";
    s[1][1] = "b";
    System.out.println("Before sorting: " + Arrays.deepToString(s));
    //Sort with java 8
    Arrays.sort(s, (a, b) -> a[0].compareTo(b[0]));
    //Sort without java 8
    for (int i = 0; i < s.length - 1; i++) {
        if (s[i][0].compareTo(s[i + 1][0]) > 0) {
            String[] temp = s[i + 1];
            s[i + 1] = s[i];
            s[i] = temp;
        }
    }
    System.out.println("After sorting: " + Arrays.deepToString(s));
}

Output:

Before sorting: [[2, a], [1, b]]

After sorting: [[1, b], [2, a]]

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

1 Comment

Hey! Thank you so much!! Can you explain how this works, and is it possible to do it without the Arrays.sort? Are we able to do it through 2D Arrays instead? Edit: This also gives me quite a bit of errors: i.imgur.com/1DAa3oK.png
0

The following code:

  public static void main(String[] args) {
    String[][] s = {
        {"Michael", "0001234567"},
        {"Jacob", "0011234567"},
        {"Robert", "0019234568"}
    };

    print(s);

    Arrays.sort(s, (a, b) -> a[0].compareTo(b[0]));
    print(s);
    Arrays.sort(s, (a, b) -> a[1].compareTo(b[1]));
    print(s);
  }

  private static void print(String[][] s) {
    for (String[] r : s) {
      System.out.println(r[0] + " - " + r[1]);
    }
    System.out.println();
  }

Will produce this output:

Michael - 0001234567
Jacob - 0011234567
Robert - 0019234568

Jacob - 0011234567
Michael - 0001234567
Robert - 0019234568

Michael - 0001234567
Jacob - 0011234567
Robert - 0019234568

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.