Solved

sorting list alphabetically

Posted on 2000-03-29
4
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

688 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