473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sorting strings efficient and elegant? but how?

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
Nov 16 '05 #1
3 1910
Daniel Weinand <sh******@gmx.de> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel Weinand" <sh******@gmx.de> wrote in message
news:uh**************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #3
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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!)
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
9
by: C.K. | last post by:
Given an array of strings (i.e char *strings), write C code to sort the list in increasing order of string length. Run time efficiency is a primary concern. Thanks a lot!
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
8
by: DierkErdmann | last post by:
Hi ! I know that this topic has been discussed in the past, but I could not find a working solution for my problem: sorting (lists of) strings containing special characters like "ä", "ü",......
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.