Solved

sorting list alphabetically

Posted on 2000-03-29
4
366 Views
Last Modified: 2012-05-04
hi
I think this must be quite a simple question. How can i alphabetically sort a list box.Please provide some source code..I could not find in archives.
David
0
Comment
Question by:dapple
  • 3
4 Comments
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2666517
You could use A List model based on Sorted collections to store your elements. These collections are based on the java.lang.Comparable interface.

Examples of its use can be seen on the java tutorial, starting on page:

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

This example from the tutorial uses a class "Name" implementing "Comparable"

import java.util.*;

        class NameSort {
            public static void main(String args[]) {
                Name n[] = {
                    new Name("John", "Lennon"),
                    new Name("Karl", "Marx"),
                    new Name("Groucho", "Marx"),
                    new Name("Oscar", "Grouch")
                };
                List l = Arrays.asList(n);
                Collections.sort(l);
                System.out.println(l);
            }
        }

    If you run this program, here's what it prints:

        [Oscar Grouch, John Lennon, Groucho Marx, Karl Marx]
0
 
LVL 2

Accepted Solution

by:
AlexJ030200 earned 50 total points
ID: 2666520
You could use A List model based on Sorted collections to store your elements. These collections are based on the java.lang.Comparable interface.

Examples of its use can be seen on the java tutorial, starting on page:

http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

This example from the tutorial uses a class "Name" implementing "Comparable"

import java.util.*;

        class NameSort {
            public static void main(String args[]) {
                Name n[] = {
                    new Name("John", "Lennon"),
                    new Name("Karl", "Marx"),
                    new Name("Groucho", "Marx"),
                    new Name("Oscar", "Grouch")
                };
                List l = Arrays.asList(n);
                Collections.sort(l);
                System.out.println(l);
            }
        }

    If you run this program, here's what it prints:

        [Oscar Grouch, John Lennon, Groucho Marx, Karl Marx]
0
 
LVL 1

Author Comment

by:dapple
ID: 2678188
thanks for your help
David
0
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2678874
You are welcome.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 141
what is the difference between "sudo su" and "su - root" 6 106
custom annotations 9 33
split string containing \r\n in Java 46 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now