× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Thank you for writing that out for me Jeff!  I was thinking that an OO
approach would be the best, but I wasn't quite sure how to do it based on
David's and Glen's responses.  This will be a good one for the archives.
 
BTW, I got the bubble sort to work with the Vectors.  I had it coded
slightly wrong.  I have included the working code to do a bubble sort on a
Vector of objects.
 
Thanks again to all who responded,
Aaron Bartell
 
for (int i = 0; i <= vAllCmp.size() - 1; i++) {

    for (int j = 0; j <= (vAllCmp.size() - i) - 2; j++) {

        IRGPerf temp1 = (IRGPerf) vAllCmp.get(j);

        IRGPerf temp2 = (IRGPerf) vAllCmp.get(j + 1);

        if (temp2.strNbr < temp1.strNbr) {

            vAllCmp.set(j, temp2);

            vAllCmp.set(j + 1, temp1);

        }

    }

}

-----Original Message-----
From: furgalj@xxxxxxxxxxxxxxxx [mailto:furgalj@xxxxxxxxxxxxxxxx]
Sent: Tuesday, February 17, 2004 11:03 AM
To: ALBartell@xxxxxxxxxxxxxx; java400-l@xxxxxxxxxxxx
Subject: Re: Sorting a Vector...



You really need to adopt the OO approach and define your IRGPerf class to
implement the Comparator interface. Just declare your class as 

class IRGPerf implements Comparator {... 

Then, somewhere in your IRGPerf class you declare and equals and a compare
method. So for your case you would have something like: 

public int compare(Object obj1, Object obj2) { 
        if (!(obj1 instanceof IRGPerf)) { 
                return 0; // Leave unknown object alone, or throw an
exception here 
        } 
        if (!(obj2 instanceof IRGPerf)) { 
                return 0; // Leave unknown object alone, or throw an
exception here 
        } 
        IRGPerf irgPerf1 = (IRGPerf)obj1; 
        IRGPerf irgPerf2 = (IRGPerf)obj2; 

        if (irgPerf1.strNbr > irgPerf2.strNbr) { 
                return 1; 
        } else if (irgPerf1.strNbr == irgPerf2.strNbr) { 
                return 0; 
        } else (irgPerf1.strNbr > irgPerf2.strNbr) { 
                return -1; 
        } 
} 

Note: if you don't like the multiple return style, just change this to use a
local result variable. 

public boolean equals(Object object) { 
        if (!(object instanceof IRGPerf)) { 
                return false; // Obviously a comparision of an unknown
object should fail 
        } 

        IRGPerf anIRGPerf = (IRGPerf)object; 
        return (this.strNbr == anIRGPerf.strNbr); 
} 

Feel free to yell at me for any syntax or procedural errors. 

Then, where you need it, use a ListArray instead of a Vector (or convert
it). Then just code: 

Collections.sort(yourList); 

It's that simple! Your Comparator methods in the IRGPerf class will be
called to compare the internal object values. 

I have similar code to this which sorts fairly large lists inside of a GUI.
Even on an average system the Java Collections sort method has sorted lists
of 10,000 in a second or less. 


Jeff Furgal
Product Architect
Lakeview Technology, Inc.

"Premature optimization is the root of all evil" - Donald Knuth




As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.