sorting strings by descending order of their lengths

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2338 Views - Last Post: 22 January 2010 - 11:43 AM Rate Topic: -----

#1 wil4cplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 07-May 09

sorting strings by descending order of their lengths

Posted 21 January 2010 - 02:18 PM

Hi,
Suppose I have Strings such as these
String str1 = "M.................*";
String str2 = "M...................................*"
String str3 = "M.........*;
String str4 = "M...........................*"



I have tried to put them in a list to sort them in descending order of their lengths. So I have written a method compareTo(List list) that returns -1, 0, 1 if the length is less than, equal and greater than in the comarison eg. str1.compareTo(str1). Then used sort() which works because my class impliments comparable interface

Im not clear about what Im doing here so could somebody explain step by step what I should to do.

Thanks!!!

Is This A Good Question/Topic? 0
  • +

Replies To: sorting strings by descending order of their lengths

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 02:47 PM

View Postwil4cplus, on 21 Jan, 2010 - 01:18 PM, said:

So I have written a method compareTo(List list) that returns -1, 0, 1 if the length is less than, equal and greater than in the comarison eg. str1.compareTo(str1).

Would like to see that class... must be something like
class MyString implements Comparable<MyString> {
	private String theString;

	public int compareTo(MyString other) {
		return theString.length() - other.theString.length();
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,987
  • Joined: 20-September 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:03 PM

It might be simpler to use a Comparator:

Collections.sort(yourListOfString, new Comparator<String>() {
	public int compare(String s1, String s2) {
		return s1.length() - s2.length();
	}
});



Was This Post Helpful? 1
  • +
  • -

#4 wil4cplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 07-May 09

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:22 PM

View Postg00se, on 21 Jan, 2010 - 02:03 PM, said:

It might be simpler to use a Comparator:

Collections.sort(yourListOfString, new Comparator<String>() {
	public int compare(String s1, String s2) {
		return s1.length() - s2.length();
	}
});





Cheers for the reply but how would this work for many strings being compared and sorted in descending order of lengths. Sorry im a novice
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:25 PM

String[] myString = new String[400];
/// fill the strings

// and then G00se code
Collections.sort(myString, new Comparator<String>() {
	public int compare(String s1, String s2) {
		return s1.length() - s2.length();
	}
});



Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:26 PM

As g00se said, use a Comparator, as String cannot be subclassed. If you look closely at the code he gave you, you will see yourListOfString, which means that you need an array.

String str1 = "M.................*";
String str2 = "M...................................*"
String str3 = "M.........*;
String str4 = "M...........................*"



Can easily be made into an array. If you have a homework problem this hard, you should know enough about arrays to turn the above into a valid array ;)

edit: ninja'd by pbl ;)

This post has been edited by Dogstopper: 21 January 2010 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,987
  • Joined: 20-September 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:31 PM

You can change the order:

return s1.length() - s2.length();



return s2.length() - s1.length();


Was This Post Helpful? 0
  • +
  • -

#8 wil4cplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 07-May 09

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:40 PM

View Postpbl, on 21 Jan, 2010 - 01:47 PM, said:

View Postwil4cplus, on 21 Jan, 2010 - 01:18 PM, said:

So I have written a method compareTo(List list) that returns -1, 0, 1 if the length is less than, equal and greater than in the comarison eg. str1.compareTo(str1).

Would like to see that class... must be something like
class MyString implements Comparable<MyString> {
	private String theString;

	public int compareTo(MyString other) {
		return theString.length() - other.theString.length();
	}
}



Thanks for responding, My class looks like this: DOES NOT compile yet but im worried about getting the algorithm right first.
class StringObj implements Comparable<StringObj> {
	
private String str;
private int myLength;

public int compareTo(StringObj str) {
		
		if (this.stringLength < str.StringLength) {
			return -1;
		}
		if (this.stringLength == str.myLength) {
			return 0;
		}
		assert this.stringLength > str.myLength;
			return 1;
}
for (int i=0; i<str.length(); i++) {
	  (str.get(i).compareTo(str.get(i+1))==0) == (str.get(i).equals(orf(i)));
}
Collections.sort(str); 	


not sure how I can make it better. Also how do I use this with Coll

This post has been edited by wil4cplus: 21 January 2010 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:46 PM

If you do it pbl's way, then at least make it simpler...

class StringObj implements Comparable<StringObj> {
	
private String str;

// The compareTo method does NOT return -1, 0, and 1 as thought by most
// novice programmers. Instead, it returns less than 0, 0, and greater than 0.
public int compareTo(StringObj str) {
	   return str.length - str.getString().length;  
}

// String access
public String getString() {
	return str;
}

// These need to be in methods...
for (int i=0; i<str.length(); i++) {
	  (str.get(i).compareTo(str.get(i+1))==0) == (str.get(i).equals(orf(i)));
}
Collections.sort(str); 



Notice that instead of checking for -1, 0, and 1, you need to check for < 0, 0, > 0.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,258
  • Joined: 27-December 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:49 PM

Let's start with your compareTo() method. I think you missed out on what everyone else was saying. First, I think they assumed that you would know that you needed to add a getter method for your String variable in the StringObj class. Next, with your compareTo() method, you need to change it around a little (as shown below and in previous posts). You do not need an assert statement in there nor can you reference a variable called strLength, stringLength or any variation on these as none of these variables exist. Since the String class has a defined length() method, you don't need the myLength attribute in the class either as it is redundant.
public int compareTo(StringObj str) {
	//assuming you define the getter method for the String str attribute as getString()
	return this.str.length() - str.getString().length();
}



So then outside of your StringObj class, you will have some sort of List<StringObj>. All you have to do is call Collections.sort() on that List object. Again, the List<StringObj> variable should not be contained within your StringObj class.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:53 PM

[code]
class StringObj implements Comparable<StringObj> {
	
private String str;
private int myLength;

public int compareTo(StringObj str) {
		
		if (this.stringLength < str.StringLength) {
			return -1;
		}
		if (this.stringLength == str.myLength) {
			return 0;
		}
		assert this.stringLength > str.myLength;
			return 1;
}


First it is myLength not StringLength
Second you can do it at run time str.length() better than having a myLength that you have to synchronized
Third no need to compare and assert. Returning str.length() - str.str.length() would do
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 03:57 PM

On the topic of assertions, I advise you to read here on opinions of them: http://www.dreaminco...topic147934.htm
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 04:01 PM

View PostDogstopper, on 21 Jan, 2010 - 02:57 PM, said:

On the topic of assertions, I advise you to read here on opinions of them: http://www.dreaminco...topic147934.htm

This assert is really useless
2 String length can only be < == or >
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 04:03 PM

View Postpbl, on 21 Jan, 2010 - 05:01 PM, said:

View PostDogstopper, on 21 Jan, 2010 - 02:57 PM, said:

On the topic of assertions, I advise you to read here on opinions of them: http://www.dreaminco...topic147934.htm

This assert is really useless
2 String length can only be < == or >


I agree, that's why I gave the link...in there, we establish that asserts IN GENERAL are worthless. And yes, in my comments, I say that a String length can only be < == or >. :)
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,258
  • Joined: 27-December 08

Re: sorting strings by descending order of their lengths

Posted 21 January 2010 - 04:05 PM

@OP: You might want to read up on the basics of assertions and some appropriate uses. They are generally used to enforce preconditions, not to test a condition and return a value (like you are doing in compareTo()). I found a link that you may find helpful with this.

Here it is: http://www.javabeat....tion-basics.php

@Edit: Ninja'd twice. I agree with the consensus that Assertions are generally useless, but if you're going to use them, you should understand how to correctly use them. :)

This post has been edited by macosxnerd101: 21 January 2010 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2