• 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,577 (1.01/day)
Likes
533
Location
Huddersfield, uk
Processor E8200
Motherboard Asus P5KC
Cooling cooler master hyper tx3, 2x 120mm, 1x 80mm
Memory 4 GB DDR3 OCZ Black Edition
Video Card(s) Gigabyte HD 2600xt silent pipe II
Storage 500GB WD Sata / 60 GB Samsung IDE
Display(s) 20" samusng LCD, gaming at 1680 x 1050
Case some old amd thing
Audio Device(s) onboard
Power Supply 650W
Software Windows 8.1, with 'start is back' obviously
Benchmark Scores It boots whilst im still young so im pleased with that.
#1
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
21,101 (6.22/day)
Likes
10,234
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#2
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,577 (1.01/day)
Likes
533
Location
Huddersfield, uk
Processor E8200
Motherboard Asus P5KC
Cooling cooler master hyper tx3, 2x 120mm, 1x 80mm
Memory 4 GB DDR3 OCZ Black Edition
Video Card(s) Gigabyte HD 2600xt silent pipe II
Storage 500GB WD Sata / 60 GB Samsung IDE
Display(s) 20" samusng LCD, gaming at 1680 x 1050
Case some old amd thing
Audio Device(s) onboard
Power Supply 650W
Software Windows 8.1, with 'start is back' obviously
Benchmark Scores It boots whilst im still young so im pleased with that.
#3
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
21,101 (6.22/day)
Likes
10,234
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#4
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,577 (1.01/day)
Likes
533
Location
Huddersfield, uk
Processor E8200
Motherboard Asus P5KC
Cooling cooler master hyper tx3, 2x 120mm, 1x 80mm
Memory 4 GB DDR3 OCZ Black Edition
Video Card(s) Gigabyte HD 2600xt silent pipe II
Storage 500GB WD Sata / 60 GB Samsung IDE
Display(s) 20" samusng LCD, gaming at 1680 x 1050
Case some old amd thing
Audio Device(s) onboard
Power Supply 650W
Software Windows 8.1, with 'start is back' obviously
Benchmark Scores It boots whilst im still young so im pleased with that.
#5
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
21,101 (6.22/day)
Likes
10,234
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#6
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.