• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

ordering strings alphanumerically

Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
Hi all

I have a question for you all.

Im trying to order a set of input data. Its a list of strings some containing just number, some just letters and some a mixture. Im coding in c# using i believe asp.net. Its a .net technology. Sorry for the vagueness there but i have only been using it for half a week and my dam windows has died and im writing this from a Ubuntu live cd (gotta love linux).

Any who ive currently managed to split the input list into three seperate list using some regex and converted the numbers into int using int.Parse(). Then ordered the numbers using .order() so that side of things is grand. I now have a list of strings containing just numbers which i order again using .order() and it orders them alphabetically, then using .addrange() i can combine the two together listing all the numbers then all the letters alphabetically at the end. However ive come a bit unstuck with the strings that contain letters and numbers.

for example i have

2test
3test
11test
4ab
1ab
32ab

which i then need listed as

1ab
4ab
32ab
2test
3test
11test

the issue is the number could be on the end or at the front. I want them to be ordered primarely alphabetically then if theres many words starting with T for example they are block together and run numercially.

So when its all finished i end up with a list like this

1
2
5
8
22
32
a
abc
bhg
2bh
bh4
bh5
h
j
3j
j5


Hope all this makes sense im in the process of fixing windows 8 AGAIN so should be able to provide more info if needed. Hope you all can shed some light onto this. Im stuck on ordering the string containing both letters and numbers
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
The easiest way to accomplish that is via a custom IComparer or IComparable. There's an example here:
http://msdn.microsoft.com/en-us/library/234b841s.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Basically your comparer returns an 32-bit signed integer that returns 0 if a and b are equal, greater than zero if a should be higher than b, and less than zero if a should be less than b.


Of course the easiest solution may be to move the numbers to the rear of the string then just use the basic string compare.
 
Last edited:
Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
Hi thanks. Could you explain the compare a greater then b bit further I don't fully understand what you mean by a greater then b. my comps fully died now so carnt google it. Having to use dam iphone app
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Let me just pull an example from one of my programs...
Code:
using System.Collections.Generic;

namespace JbnLib.Shared
{
    public class OffsetLengthPairComparer : IComparer<OffsetLengthPair>
    {
        public int Compare(OffsetLengthPair a, OffsetLengthPair b)
        {
            if (a.Offset > b.Offset)
                return 1;
            else if (a.Offset < b.Offset)
                return -1;
            else
                return 0;
        }
    }
}
It compares a to b and depending on what it returns, places a before b, a after b, or no change. To invert it, simply swap that negative sign.

In your program, you'll want to first compare the string component, then compare the numeric component. Remember, if you use Convert.ToInt32 (or similar), it ignores all non-numeric characters.
 
Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
Oh now I'm with you. Stick that in a for each loop going through my lists and itl place them in order just condition like you said to go through string then numerical components. Great. Thank you very much. :) when I get my computer up tomorrow ill have a play.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You don't loop it, it does via .Sort(IComparer). In my example, it would like like...
Code:
List<string> list = new List<string>();
list.Add("2");
list.Add("1");
list.Add("0");

//sort it
OffsetLengthPairComparer comparer = new OffsetLengthPairComparer();
list.Sort(comparer);
Obviously my comparer doesn't work for strings because it is for a custom type but, you get the idea.
 
Top