Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COmparator with strings that contains numbers, * and #

Status
Not open for further replies.

celia05es

Programmer
Jan 10, 2002
81
US
Hello,
I have been trying to solve my problem since this morning but I cannot do it!!!
Please, help me.
I have an array. Each element can contain digits, "*" or/and "#". I would like to sort the array using comparator... but I can't seem to find the correct algorithm.

COuld you help me?

Thanks
ELisabeth
 
Ok, let's clarify some points:

1.- You can only convert to numbers Strings that contain just numbers.

2.- By default, Strings are sorted alphabetically.

3.- If you want to sort Strings that represents numbers, a way is converting them to numbers. That won't work for mixed Strings (letters + digits)

4.- If you want a differente sorting algoritmh than the default one, you have to implement it via the Comparable interface or any other mechanism.

Cheers,

Dian

 
THat's the problem I am having. I have tried to implement the sort wia the Comparable interface but it is not working correctly. COuld you help me with it?
 
No, because the strings can contain letters and numbers.
So, I need to have them sorted correctly.
For instance:
100
1001
900
user1
user2
user11
etc.
 
Ok, define your WHOLE problem with ALL requirements, not just pieces of it, and I'll try to give you some help.

For example:

"100" < "user1" ?
"100!" < "100a" ?
"1001" < "900" ?

Cheers,

Dian
 
Fair enough!
Here is what I think would do:
- The strings will only contain letters and numbers.
- The number order should be respected:
100<200<900<1001
- The alphabetical order should be respected too:
foo<user<user1< user10
For instance:
100
100a
100ab
100u
1a300
200
201a
...
usu
usu1
....

Thank you
 
uname -a" gives: "SunOS out 5.7 Generic_106541-12 sun4u sparc SUNW,Ultra-5_10"

 
Right,

the easiest way is to use the native "sort" program.

Create a file with your to_be_sorted stuff in it ("abc.txt"), and then run this code :

Code:
public class Test {
 	public static void main(String args[]) throws Exception {
		String file = "abc.txt";
		String fileOut = "abc_sorted.txt";
		Process p = Runtime.getRuntime().exec("sort " +file +" -o " +fileOut);
		p.waitFor();
       }
}

Ta Da !



--------------------------------------------------
Free Database Connection Pooling Software
 
That is a good idea but now how do I include this in my code!!
I mean, I have a file.java with a lot of rutines. A lot of rutines will use this sort, so this sort has to be a routine that can be called.
I have tried to compile the code and run it but I must do something wrong because when I try to execute it (java Test)I get:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
 
set your CLASSPATH to include your directory where the Test.class is.

--------------------------------------------------
Free Database Connection Pooling Software
 
How stupid of me!!!!
OK, it does exactly what I need....
Now, how can I include it in my routines?

Something else... I don't have a file but a Vector.
 
Well, you would have to write out the contents of the vector to a file, sort it, and then read the contents back into a vector.

You do realise this is probably a really, really inefficient and stupid way of doing it though right ?

--------------------------------------------------
Free Database Connection Pooling Software
 
I know that 's why I wanted to write a Comparator....
Any idea about how I could write a Comparator?
 
Can you pad the numbers in the strings with zeros?
If so, the library provided comparisons will work...

0100 < 0900 < 1001 < user1 < user2 < user11

So, the order you want is
- In alpha order
- If the first char is numaric grab the whole first number and compare as an int
And you say they are only alpha numaric, but were talking about * and # ... So, which is it?

Alright how about this (in psuedo code)

Code:
if(one starts with a digit and other doesn't)
  return that the one with digit is smaller
if(first char of both are digits)
{
  get the whole first number (all chars that are digits) from both 
  if the numbers are equal 
    continue 
  else return the result
}
else
{
  compare the string, striping all numbers that may be in middle or end of string (storing them for comparison)
  if(strings w/o nums equal)
      compare the numbers you striped out
  else return the inequality
}

Why is this sorted order so important, and why are there so many numbers in all different places in the string? It seems that there should be a little more consistancy to the strings in most applications... To be honest you may want to consider writing several small/simpler comparitors and just sort using each untill the order and sub order and sub-suborder are exactly as you want...

Is al10fred < al020fred or al020fred < al10fred
10 < 020 in numbers, but asciibetical and alphabetical (by most standards) 020 < 10.

Just some food for thought
 
The list you gave is ordered alphabetically.

That can be done through java.util.Arrays.sost(String[]) method.

Cheers,

Dian
 
Thank you very much for the pseudo code. I will come back to my customer and ask him about some more restrictive rules.
Thank you so much for your time....

MERRY CHRISTMAS to everyone.

Elisabeth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top