Results 1 to 4 of 4
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default How would I accomplish this?

    I am trying to find a concise way to write the sort methods for my class.
    For an assignment, I am supposed to make a program that can sort objects by a number of fields: year, rank, artist and title.

    I used an idea from this thread:
    java - Sorting a collection of objects - Stack Overflow

    and I am trying to use the custom comparator for my sort methods. However for some reason, the sortingBy variable fails to recognize any of the enum types.
    Whenever I try to set the sortingBy variable equal to one of them, for example:
    Java Code:
    private Order sortingBy = Year;
    I get a "Year cannot be resolved to a variable" error.

    What I want to be able to do is make it so every time a specific method is called, say, for example sortTitle(), sortingBy will change to Title, then the SongComparator will sort using the case Title.
    Is it possible to do this? I can't figure out how to modify SongComparator's object variables that way.
    Java Code:
    import java.util.Comparator;
    
    public class SongComparator implements Comparator<Song> {
    	public enum Order {Year, Rank, Artist, Title}
    	  public Order sortingBy;
    
    	  @Override
    	  public int compare(Song song1, Song song2) {
    	    switch(sortingBy) {
    	      case Year: return Integer.compare(song1.year, song2.year);
    	      case Rank: return Integer.compare(song1.rank, song2.rank);
    	      case Artist: return song1.artist.compareTo(song2.artist);
    	      case Title: return song1.title.compareTo(song2.title);
    	    }
    	    throw new RuntimeException("Practically unreachable code, can't be thrown");
    	  }
    
    	  public void setSortingBy(Order sortBy) {
    	    this.sortingBy = sortingBy;
    	  }
    	}
    SongCollection class that contains sort methods
    Java Code:
    public class SongCollection {
    	ArrayList<Song> songs2;
    	ArrayList<Song> itemsToRemove = new ArrayList<Song>(); // second collection
    															// for items to
    															// remove
    
    	public SongCollection(ArrayList<Song> songs) { // constructor for
    													// SongCollection
    		this.songs2 = songs;
    	}
    
    	public void filterYear(Range r) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if (song1.year > (r.getMax()) || (song1.year) < (r.getMin())) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void filterRank(Range r) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if (song1.rank > (r.getMax()) || (song1.rank) < (r.getMin())) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void filterArtist(String s) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if ((!(((song1.getArtist()).contains(s))))) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void filterTitle(String s) {
    		int n = 0;
    		if (n == 0) {
    			System.out.println("Program is processing.");
    			n++;
    			for (Song song1 : songs2) {
    				if ((!(((song1.getTitle()).contains(s))))) {
    					itemsToRemove.add(song1);
    				}
    			}
    			songs2.removeAll(itemsToRemove);
    			itemsToRemove.clear();
    		}
    	}
    
    	public void sortTitle() {
    		SongComparator comparator = new SongComparator();
    		SongComparator.Order(sortingBy) = Title;
    		comparator.setSortingBy(sortingBy);
    		Collections.sort(songs2, comparator);
    	}
    	public void sortRank() {
    
    	}
    
    	public void sortArtist() {
    
    	}
    
    	public void sortYear() {
    
    	}
    
    	public String toString() {
    		String result = "";
    		for (int i = 0; i < songs2.size(); i++) {
    			result += " " + songs2.get(i);
    		}
    
    		return result;
    
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,402
    Rep Power
    25

    Default Re: How would I accomplish this?

    The posted code is missing class definitions for compilation testing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: How would I accomplish this?

    Java Code:
    import java.util.StringTokenizer;
    
    public class Range {
    	private int min;
    	private int max;
    
    	public Range() {
    		System.out.println("Please wait.");
    	}
    
    	public static Range parse(String s) {
    		Range instance = new Range(); // instance is created here so object
    										// variables may be accessed
    		String field; // String to contain deleted part of user input
    		StringTokenizer tokenizer = new StringTokenizer(s, "-");
    		StringTokenizer tokenizer2 = new StringTokenizer(s, ":");// for separating "field:" from the
    																	// other part of the String
    		if (s.contains(":")) { // this deletes the "field:" of the user input so
    								// it does not interfere with the parsing
    			field = (tokenizer2.nextToken());
    			s = s.replace(field, "");
    			s = s.replace(":", "");
    		}
    		if (s.contains("-")) {
    			instance.min = Integer.parseInt(tokenizer.nextToken());
    			instance.max = Integer.parseInt(tokenizer.nextToken());
    
    		} else if (!(s.contains("-"))) {
    			{
    				instance.min = Integer.parseInt(s);
    				instance.max = Integer.parseInt(s);
    			}
    		}
    		System.out.println("Range max = " + instance.max);
    		System.out.println("Range min = " + instance.min);
    		return instance;
    	}
    
    	public boolean contains(int n) {
    		if (n > min && n < max) { //if the number is contained in the range, method returns true.
    			return true;
    		} else if (n == min && n == max) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    
    	public int getMin() {
    		return min;
    	}
    
    	public int getMax() {
    		return max;
    	}
    }
    Java Code:
    public class Song{
    	public enum Order {Year, Rank, Title, Artist}
    	public int year;
    	public int rank;
    	public String artist;
    	public String title;
    
    	public static Song parse(String s) {
    		Song instance = new Song();
    		StringTokenizer tokenizer = new StringTokenizer(s, "\t");
    		instance.year = Integer.parseInt(tokenizer.nextToken());
    		instance.rank = Integer.parseInt(tokenizer.nextToken());
    		instance.artist = (tokenizer.nextToken());
    		instance.title = (tokenizer.nextToken());
    		return instance;
    	}
    
    	public int getYear() {
    		return year;
    	}
    
    	public int getRank() {
    		return rank;
    	}
    
    	public String getArtist() {
    		return artist;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public String toString() {
    		String output = "\n\nYear = " + year + "\nRank = " + rank + "\nArtist = "
    				+ artist + "\nTitle = " + title;
    		return output;
    	}
    	
    	}

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,402
    Rep Power
    25

    Default Re: How would I accomplish this?

    Try adding the prefix: Order.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 11-20-2012, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •