Solved

sorting list alphabetically

Posted on 2000-03-29
4
349 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 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

18 Experts available now in Live!

Get 1:1 Help Now