The Artima Developer Community
Sponsored Link

Java Answers Forum
sorting alphabetically

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
JamesMoore

Posts: 2
Nickname: jamesmoore
Registered: Dec, 2002

sorting alphabetically Posted: Dec 23, 2002 6:06 AM
Reply to this message Reply
Advertisement
I have thought and thought but the sorting alphabetically doesnt work. There are two classes Record and CDRack.
I can't use Collections.sort and class Record doesnt implement Comparable(and I can't change it).

One suggestion is that I could make my own class which implements Comparator.


import java.util.*;

public class AlphabeticComparator
implements Comparator{
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
return s1.toLowerCase().compareTo(
s2.toLowerCase());
}
}


But how an earth do I "tell" to CDRack class that it would
use the class AlphabeticComparator?
All methods except the sortAlphabetically works.
And how to capture "l.toArray(mRecords)" from organize() so that I could use it also in sortAlphabetically()?

-------------------------------------------------------
/** Class CDRack represents collections of compact discs. Discs are
located in the rack in slots numbered from zero upwards. The discs are
represented by Record objects and empty slots by null values. */


public class CDRack extends Object {

private Record[] mRecords;
private int size;


/**Creates a new, empty CD rack.
Parameters:
size - the size of the new rack, i.e. the number of slots it has */

public CDRack(int size) {
mRecords = new Record[size];
this.size = size;
}


/** "Organizes" the discs in the rack so that they will be located in
consecutive slots starting at slot number zero, and any and all empty
slots will be at the "end of the rack". After calling this method, the
discs are in an undefined (i.e. arbitrary) order - the only thing this
method guarantees is that there aren't any empty slots in between full
ones.
*/


public void organize() {

// Turn array into a list - more flexible
List l = Arrays.asList(aanilevyt);

// Remove all nulls from a copy of the list which supports removal.
l = new ArrayList(l);
while (l.remove(null)) /*do nothing*/;

// Clear the original array.
for (int i = 0; i < mRecords.length; i++){
mRecords<i> = null;
} // Put the non-nulls back.

l.toArray(mRecords);
}




/**"Organizes" the discs in the rack to the beginning of the rack (see
the method organize) and sorts them in alphabetical order. Recordings
by the same artist are placed in alphabetical order by disc name. */

public void sortAlphabetically() {
}

------------------------------------------------------

Thanks for your time! I wish you a merry Christmas!

Topic: trying to write a program that's like Minesweeper... stuck on gui Previous Topic   Next Topic Topic: How to write an client applet using servlet , through tomcat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use