473,586 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ 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 1919
Daniel Weinand <sh******@gmx.d e> 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.co m>
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.Compa re(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(strXPa rts.Length,
strYParts.Lengt h); ++index)
{
// Convert each part to an integer.
xValue = int.Parse(strXP arts[index]);
yValue = int.Parse(strYP arts[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.Leng th != strYParts.Lengt h)
{
// Check the lengths.
if (strXParts.Leng th > strYParts.Lengt h)
{
// 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.co m

"Daniel Weinand" <sh******@gmx.d e> wrote in message
news:uh******** ******@TK2MSFTN GP10.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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
6033
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 containing norwegian special characters æ, ø and å. >>> liste =
4
2526
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() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
7
3253
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)) CType(local4, Short) = CType(src, Short)
18
3036
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 frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary...
22
4132
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) { return -1; } else
9
14351
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
6398
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 <stdio.h> void main(void) { char lines; int count = 0, i;
8
8830
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 "ä", "ü",... (german umlaute). Consider the following list: l = For sorting the letter "Ä" is supposed to be treated like "Ae", therefore sorting this list...
1
7179
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find...
1
7954
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.