How do I sort an Enumeration?

Category: java.util, viewed: 824 time(s).
package org.kodejava.example.util;

import java.util.*;

public class EnumerationSort {
    public static void main(String[] args) {
        //
        // Creates random data for sorting source. Will use java.util.Vector
        // to store the random integer generated.
        //
        Random random = new Random();
        Vector<Integer> data = new Vector<Integer>();
        for (int i = 0; i < 10; i++) {
            data.add(Math.abs(random.nextInt()));
        }

        //
        // Get the enumeration from the vector object and convert it into
        // a java.util.List. Finally we sort the list using
        // Collections.sort() method.
        //
        Enumeration enumeration = data.elements();
        List<Integer> list = Collections.list(enumeration);
        Collections.sort(list);

        //
        // Prints out all generated number after sorted.
        //
        for (Integer number : list) {
            System.out.println("number = " + number);
        }
    }
}

An example result of the code above is:

number = 64029842
number = 72921309
number = 230054456
number = 256151404
number = 345486128
number = 387933255
number = 398974777
number = 626349261
number = 654063730
number = 1319397578
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats