?
Solved

sorting list alphabetically

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

765 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