Click here to Skip to main content
15,891,905 members
Home / Discussions / C#
   

C#

 
QuestionOut of Memory error while generating TreeView Pin
Member 768043413-Feb-14 2:04
Member 768043413-Feb-14 2:04 
AnswerRe: Out of Memory error while generating TreeView Pin
Dave Kreskowiak13-Feb-14 2:35
mveDave Kreskowiak13-Feb-14 2:35 
AnswerRe: Out of Memory error while generating TreeView Pin
Eddy Vluggen13-Feb-14 2:59
professionalEddy Vluggen13-Feb-14 2:59 
AnswerRe: Out of Memory error while generating TreeView Pin
BillWoodruff13-Feb-14 3:24
professionalBillWoodruff13-Feb-14 3:24 
QuestionNumericString Sort Pin
Member 1059316812-Feb-14 20:52
Member 1059316812-Feb-14 20:52 
AnswerRe: NumericString Sort Pin
V.12-Feb-14 21:37
professionalV.12-Feb-14 21:37 
GeneralRe: NumericString Sort Pin
harold aptroot12-Feb-14 22:39
harold aptroot12-Feb-14 22:39 
AnswerRe: NumericString Sort Pin
DaveyM6912-Feb-14 23:37
professionalDaveyM6912-Feb-14 23:37 
This works the way you have described. It may not be very efficient so you may want to optimise it somewhat:
C#
public static int Compare(string first, string second)
    {
        if (object.ReferenceEquals(first, second))
            return 0;
        if (object.ReferenceEquals(null, first)) // second cannot be null
            return -1;
        // neither string can be null
        char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8','9' };
        int firstDigitIndex = first.IndexOfAny(digits);
        int secondDigitIndex = second.IndexOfAny(digits);
        string firstLhs = firstDigitIndex == -1 ? first : first.Substring(0, firstDigitIndex);
        string secondLhs = secondDigitIndex == -1 ? second : second.Substring(0, firstDigitIndex);
        int result = firstLhs.CompareTo(secondLhs);
        if (result == 0)
        {
            // left hand sides are equal so sort on numeric part
            int firstInteger = 0;
            int secondInteger = 0;
            if (firstDigitIndex > -1)
                int.TryParse(first.Substring(firstDigitIndex), out firstInteger);
            if (secondDigitIndex > -1)
                int.TryParse(second.Substring(secondDigitIndex), out secondInteger);
            result = firstInteger.CompareTo(secondInteger);
        }
        return result;
    }

Test:
C#
SortAndWriteList(new List<string>(new string[] { "ABC 1", "ABC 10", "ABC 2", "ABC 100" }));
Console.WriteLine();
SortAndWriteList(new List<string>(new string[] { "ADBCDEF - 1", "ADBCDEF - 10", "ADBCDEF - 3" }));
Console.WriteLine();
Console.ReadKey();

private static void SortAndWriteList(List<string> list)
{
    if (list != null)
    {
        list.Sort(new Comparison<string>(Compare));
        foreach (string item in list)
            Console.WriteLine(item);
    }
}

Result:
ABC 1
ABC 2
ABC 10
ABC 100

ADBCDEF - 1
ADBCDEF - 3
ADBCDEF - 10

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



AnswerRe: NumericString Sort Pin
Richard Deeming13-Feb-14 0:50
mveRichard Deeming13-Feb-14 0:50 
QuestionHow to Serialize & De-Serialize Any Controls Property in C# Pin
Tridip Bhattacharjee12-Feb-14 20:26
professionalTridip Bhattacharjee12-Feb-14 20:26 
AnswerRe: How to Serialize & De-Serialize Any Controls Property in C# Pin
BillWoodruff12-Feb-14 22:57
professionalBillWoodruff12-Feb-14 22:57 
QuestionWhat is the difference between JavaScript 'var' and C# 'dynamic' Pin
Sanju Uthaiah Bollera12-Feb-14 20:08
Sanju Uthaiah Bollera12-Feb-14 20:08 
AnswerRe: What is the difference between JavaScript 'var' and C# 'dynamic' Pin
Richard MacCutchan12-Feb-14 21:41
mveRichard MacCutchan12-Feb-14 21:41 
AnswerRe: What is the difference between JavaScript 'var' and C# 'dynamic' Pin
BillWoodruff12-Feb-14 22:40
professionalBillWoodruff12-Feb-14 22:40 
Questiondatagridview combobox Pin
abdul rafi12-Feb-14 19:29
abdul rafi12-Feb-14 19:29 
AnswerRe: datagridview combobox Pin
Eddy Vluggen13-Feb-14 9:06
professionalEddy Vluggen13-Feb-14 9:06 
Question3D Model Pin
Kadam dIgambar12-Feb-14 18:07
Kadam dIgambar12-Feb-14 18:07 
SuggestionRe: 3D Model Pin
Richard MacCutchan12-Feb-14 21:39
mveRichard MacCutchan12-Feb-14 21:39 
GeneralRe: 3D Model Pin
JV999914-Feb-14 3:07
professionalJV999914-Feb-14 3:07 
QuestionConverting Back from Decimal to Byte Pin
computerpublic12-Feb-14 10:03
computerpublic12-Feb-14 10:03 
AnswerRe: Converting Back from Decimal to Byte Pin
Richard Deeming12-Feb-14 11:07
mveRichard Deeming12-Feb-14 11:07 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic12-Feb-14 11:29
computerpublic12-Feb-14 11:29 
GeneralRe: Converting Back from Decimal to Byte Pin
Richard Deeming13-Feb-14 0:37
mveRichard Deeming13-Feb-14 0:37 
GeneralRe: Converting Back from Decimal to Byte Pin
computerpublic13-Feb-14 9:15
computerpublic13-Feb-14 9:15 
GeneralRe: Converting Back from Decimal to Byte Pin
Richard Deeming13-Feb-14 10:35
mveRichard Deeming13-Feb-14 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.