Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sorting list alphabetically

Posted on 2000-03-29
4
Medium Priority
?
434 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.
Suggested Courses

596 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