sorting strings efficient and elegant? but how?

  • Thread starter Thread starter Daniel Weinand
  • Start date Start date
D

Daniel Weinand

hello ng,

i have a problem and a imho an insufficient method of solution.
strings should be sorted by specific text pattern and dispayed
in groups. the strings are stored in a db and have the following layout:

1.0.0.0
1.1.0.0
1.1.1.0
1.1.2.0
1.1.17.0
1.2.0.0
1.2.1.0
1.2.3.0
1.2.29.0
1.7.0.0
1.7.4.0
1.7.5.0
2.13.12.0
2.14.1.0
17.255.1.0
17.255.2.0
12.255.24.0
....
'n'.'n'.'n'.'n'

the problem is now to sort the strings by groups.
sort key is either the first digit or the the first and
the second, or the first, the second and the third.
example:
if sort key is first digit, the following strings should be displayed.
1.0.0.0
1.1.0.0
1.1.1.0
1.1.2.0
1.1.17.0
1.2.0.0
1.2.1.0
1.2.3.0
1.2.29.0
1.7.0.0
1.7.4.0
1.7.5.0
according for digits 1...n. in worst case there can be all possible int
values.

should be searched an sorted for '1.1.x.x the values should be:
1.1.0.0
1.1.1.0
1.1.2.0
1.1.17.0

and so on....

at any time during runtime the required strings will be defined.

how can i solve this efficient and elegant?

my first method of solution is using array and loops.
using split() to separate the single values and verifying them.

it's half cooked for now, but its to forsee that it will end in a loop orgy.
that's neither volitional by myself nor by anyone else.
getting the values via 'sql like' is neither possible nor desired.
generally for sure, but not in
this special case.

a simple but ingenious solution is prefered. ;) unfortunately c# and
dotnet is pretty new for me,
so that i don't have the faintest idea how to solve this in a good manner.

thought about regular expressions or something like that.

perhaps anyone had a similiar problem and can help me with his ingenious
idea.

proposals? suggestions?


greetings

Daniel
 
Daniel Weinand said:
i have a problem and a imho an insufficient method of solution.
strings should be sorted by specific text pattern and dispayed
in groups. the strings are stored in a db and have the following layout:

1.0.0.0
1.1.0.0
1.1.1.0
1.1.2.0
1.1.17.0
1.2.0.0
1.2.1.0
1.2.3.0
1.2.29.0
1.7.0.0
1.7.4.0
1.7.5.0
2.13.12.0
2.14.1.0
17.255.1.0
17.255.2.0
12.255.24.0
...
'n'.'n'.'n'.'n'

If every number is in the range 0-255, I'd create a new class with the
four values in as bytes, parse the strings to instances of those
classes, use a simple implementation of IComparer to provide the
comparison, and then turn them back into strings as and when you need
them.
 
Daniel,

I would place all of these strings in an array, and then call the static
Sort method on the Array class, passing an implementation of IComparer.
This implementation would look like this:

int IComparer.Compare(object x, object y)
{
// Cast to strings.
string strX = (string) x;
string strY = (string) y;

// Now split each apart into the individual parts.
string[] strXParts = strX.Split(new char[]{'.'});
string[] strYParts = strY.Split(new char[]{'.'});

// The values.
int xValue = 0, yValue = 0;

// Cycle through the array elements, for the minimum number of elements.
for (int index = 0; index < Math.Min(strXParts.Length,
strYParts.Length); ++index)
{
// Convert each part to an integer.
xValue = int.Parse(strXParts[index]);
yValue = int.Parse(strYParts[index]);

// If x is less than y, then return -1.
// If x is greater than y, return 1.
// Otherwise, continue.
if (xValue != yValue)
{
// If x is greater than y, return 1.
if (xValue > yValue)
{
// Return 1.
return 1;
}
else
{
// Returrn -1.
return -1
}
}
}

// At this point, all parts are equal up to the min of the two lengths.
// If the lengths are the same, return zero. Otherwise, return 1 if x
has
// more parts than y, or -1 if x has less parts than y.
if (strXParts.Length != strYParts.Length)
{
// Check the lengths.
if (strXParts.Length > strYParts.Length)
{
// Return 1.
return 1;
}
else
{
// Return -1.
return -1;
}
}

// They are equal.
return 0;
}

Hope this helps.
 
If every number is in the range 0-255, I'd create a new class with the
four values in as bytes, parse the strings to instances of those
classes, use a simple implementation of IComparer to provide the
comparison, and then turn them back into strings as and when you need
them.

I'd go with your method over Nicholas's (He converting string to numbers
on every comparison; you're only doing it once per string), but with one
modification: Instead of converting them back to strings at the end, just
make the class hold 4 bytes & a string, and drag them along.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Back
Top