I need to sort the display by name in alphabetical order. How do I do that?

public class Node {
    String name;
    int age;
    String position;
    int salary;
    String num;
    Node next;

    Node() {
        name = null;
        age = 0;
        position = null;
        salary = 0;
        num = null;
        next = null;
    }

    Node(String name, int age, String position,int salary, String num, Node next) {
        this.name = name;
        this.age = age;
        this.position = position;
        this.salary = salary;
        this.num = num;
        this.next = next;
    }

    String getName() {
        return name;
    }

    int getAge() {
        return age;
    }

    String getPosition() {
        return position;
    }

    String getNum() {
        return num;
    }

     int getSalary() {
        return salary;
    }

    Node getNext() {
        return next;
    }

    void setNext (Node next) {
        this.next = next;
    }
}


import javax.swing.*;

public class LinkedList {
    Node head;

    LinkedList() {
        head = null;
    }

    Node getHead() {
        return head;
    }

    void addItem (String name, int age, String position, int salary, String num) {
        Node NewNode = new Node(name, age, position, salary, num, null);
        NewNode.setNext (head);
        head = NewNode;
        sortList (NewNode);
    }

    void sortList (Node n) {

    }

    void displayItem() {
        Node current = head;    
        String output = "";

        while (current != null) {
            output += current.name + "\n" + current.position + "\n" + current.salary + "\n\n";
            current = current.next;
        }
        System.out.print(output);
    }
}




import javax.swing.*;

public class Client {
    public static void main(String args[]) {
        int choice, age, salary;
        String name, position, num;
        LinkedList list = new LinkedList();

        do {
            String menu = "1. Add node\n2. Delete node\n3. Display node\n4. Exit";
            choice = Integer.parseInt(JOptionPane.showInputDialog (menu + "\nEnter choice"));

            switch (choice) 
            {
                case 1: name = JOptionPane.showInputDialog ("Enter name:").toUpperCase();
                age = Integer.parseInt(JOptionPane.showInputDialog ("Enter age:"));
                position = JOptionPane.showInputDialog ("Enter position:");
                salary = Integer.parseInt(JOptionPane.showInputDialog ("Enter salary:"));
                num = JOptionPane.showInputDialog ("Enter contact number:");
                list.addItem (name,age,position,salary,num);

                break;

                case 2: //list.deleteItem();
                break;

                case 3: list.displayItem();
                break;

                case 4: break;
            }
        } while (choice != 4);
    }
}

You do it exactly the same way as sorting a String array. The only different is the way you traverasal the list and the way you swap the items. If you still don't get the idea, I will come back and check on it later...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.