|
-
Oct 27th, 2005, 04:27 PM
#1
Thread Starter
Fanatic Member
String comparisons
I have a string that i want to insert into an unsorted list in the correct place alphabetically. Here is what i mean... The list:
Peter (1)
George (2)
Mike (3)
Bill (4)
Jason (5)
Carl (6)
Alex (7)
If i wanted to insert the string 'Dan' (String X) into this list the logical answer would be to compare the strings in the list. This is how i would do it.
- String 1 is more than (alphabetically) string 2, so continue
- String 2 is less than (alphabetically) string 3, so
- Compare string 2 and string X
- String X is less than (alphabetically) string 2, so continue
- String 3 is more than (alphabetically) string 4, so continue
- String 4 is less than (alphabetically) string 5, so
- Compare string 4 and string X
- String X is more than (alphabetically) string 4, so
- Move string 5,6,7 to element 6,7,8 in list, leaving element 5 blank
- Insert string X into element 5 (blank space)
My question is how would i compare the strings?
Last edited by x-ice; Feb 25th, 2007 at 07:19 PM.
-
Oct 27th, 2005, 06:29 PM
#2
Frenzied Member
Re: String comparisons
Compare Strings using .equals().
For instance, I'm assuming you mean an arraylist:
ArrayList al = new ArrayList();
//what's in it
Peter (1)
George (2)
Mike (3)
Bill (4)
Jason (5)
Carl (6)
Alex (7)
Then you would do something like the following:
Code:
if (al.get(1).toString().equals(al.get(2).toString()))
{
element 1 equals element 2;
}
If it's just a regular array, then replace .get(i) with [i].
Let me know if you don't understand.
-
Oct 27th, 2005, 06:30 PM
#3
Frenzied Member
Re: String comparisons
Oh, the reason you call toString() is because arraylist hands back objects.
-
Oct 28th, 2005, 05:01 AM
#4
Thread Starter
Fanatic Member
Re: String comparisons
 Originally Posted by System_Error
Compare Strings using .equals().
For instance, I'm assuming you mean an arraylist:
ArrayList al = new ArrayList();
//what's in it
Peter (1)
George (2)
Mike (3)
Bill (4)
Jason (5)
Carl (6)
Alex (7)
Then you would do something like the following:
Code:
if (al.get(1).toString().equals(al.get(2).toString()))
{
element 1 equals element 2;
}
If it's just a regular array, then replace .get(i) with [i].
Let me know if you don't understand.
How can i establish alphbetical position using this? How can i establish the George would come before Peter alphabetically?
-
Oct 28th, 2005, 05:15 AM
#5
Re: String comparisons
There is no correct position in an unsorted list. (Unless by unsorted list you mean a list that doesn't sort itself automatically but is, for now, sorted.)
Consider this unsorted list:
Ralph
William
Anthony
Where is the correct place to insert Thomas?
Aside from that little problem, you can lessThan() to compare strings lexicographically.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|