Sort List like Excel columns sort
Please consider this List:
"A", "C", "AB", "AD", "N", "Z", "AC"
I want to sort this string (That are being Excel column) like Excel Column Sorting.
I want the result like this:
"A", "C", "N", "Z", "AB", "AC", "AD"
Is it possible using LINQ OrderBy?
What is the best approach?
Thanks
c# linq sorting c#-4.0
|
show 1 more comment
Please consider this List:
"A", "C", "AB", "AD", "N", "Z", "AC"
I want to sort this string (That are being Excel column) like Excel Column Sorting.
I want the result like this:
"A", "C", "N", "Z", "AB", "AC", "AD"
Is it possible using LINQ OrderBy?
What is the best approach?
Thanks
c# linq sorting c#-4.0
If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.
– Mohit Shrivastava
Dec 28 '15 at 7:05
3
Just to note: This order is called quasi-lexicographic order.
– Joey
Dec 28 '15 at 7:10
If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want toSort
theList<>
in-place, you could use something likeyourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));
. If the list may containnull
values, you need to take care of that in the lambda of course.
– Jeppe Stig Nielsen
Dec 28 '15 at 7:30
@MohitShrivastava But that would make"AB"
and"BA"
equal.
– Jeppe Stig Nielsen
Dec 28 '15 at 7:33
Absolutely Right @JeppeStigNielsen..OrderBy(p=>p.Length).ThenBy(p=>p);
is the best way to do so. :)
– Mohit Shrivastava
Dec 28 '15 at 7:34
|
show 1 more comment
Please consider this List:
"A", "C", "AB", "AD", "N", "Z", "AC"
I want to sort this string (That are being Excel column) like Excel Column Sorting.
I want the result like this:
"A", "C", "N", "Z", "AB", "AC", "AD"
Is it possible using LINQ OrderBy?
What is the best approach?
Thanks
c# linq sorting c#-4.0
Please consider this List:
"A", "C", "AB", "AD", "N", "Z", "AC"
I want to sort this string (That are being Excel column) like Excel Column Sorting.
I want the result like this:
"A", "C", "N", "Z", "AB", "AC", "AD"
Is it possible using LINQ OrderBy?
What is the best approach?
Thanks
c# linq sorting c#-4.0
c# linq sorting c#-4.0
asked Dec 28 '15 at 6:58
ArianArian
4,89150150248
4,89150150248
If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.
– Mohit Shrivastava
Dec 28 '15 at 7:05
3
Just to note: This order is called quasi-lexicographic order.
– Joey
Dec 28 '15 at 7:10
If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want toSort
theList<>
in-place, you could use something likeyourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));
. If the list may containnull
values, you need to take care of that in the lambda of course.
– Jeppe Stig Nielsen
Dec 28 '15 at 7:30
@MohitShrivastava But that would make"AB"
and"BA"
equal.
– Jeppe Stig Nielsen
Dec 28 '15 at 7:33
Absolutely Right @JeppeStigNielsen..OrderBy(p=>p.Length).ThenBy(p=>p);
is the best way to do so. :)
– Mohit Shrivastava
Dec 28 '15 at 7:34
|
show 1 more comment
If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.
– Mohit Shrivastava
Dec 28 '15 at 7:05
3
Just to note: This order is called quasi-lexicographic order.
– Joey
Dec 28 '15 at 7:10
If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want toSort
theList<>
in-place, you could use something likeyourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));
. If the list may containnull
values, you need to take care of that in the lambda of course.
– Jeppe Stig Nielsen
Dec 28 '15 at 7:30
@MohitShrivastava But that would make"AB"
and"BA"
equal.
– Jeppe Stig Nielsen
Dec 28 '15 at 7:33
Absolutely Right @JeppeStigNielsen..OrderBy(p=>p.Length).ThenBy(p=>p);
is the best way to do so. :)
– Mohit Shrivastava
Dec 28 '15 at 7:34
If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.
– Mohit Shrivastava
Dec 28 '15 at 7:05
If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.
– Mohit Shrivastava
Dec 28 '15 at 7:05
3
3
Just to note: This order is called quasi-lexicographic order.
– Joey
Dec 28 '15 at 7:10
Just to note: This order is called quasi-lexicographic order.
– Joey
Dec 28 '15 at 7:10
If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to
Sort
the List<>
in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));
. If the list may contain null
values, you need to take care of that in the lambda of course.– Jeppe Stig Nielsen
Dec 28 '15 at 7:30
If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to
Sort
the List<>
in-place, you could use something like yourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));
. If the list may contain null
values, you need to take care of that in the lambda of course.– Jeppe Stig Nielsen
Dec 28 '15 at 7:30
@MohitShrivastava But that would make
"AB"
and "BA"
equal.– Jeppe Stig Nielsen
Dec 28 '15 at 7:33
@MohitShrivastava But that would make
"AB"
and "BA"
equal.– Jeppe Stig Nielsen
Dec 28 '15 at 7:33
Absolutely Right @JeppeStigNielsen.
.OrderBy(p=>p.Length).ThenBy(p=>p);
is the best way to do so. :)– Mohit Shrivastava
Dec 28 '15 at 7:34
Absolutely Right @JeppeStigNielsen.
.OrderBy(p=>p.Length).ThenBy(p=>p);
is the best way to do so. :)– Mohit Shrivastava
Dec 28 '15 at 7:34
|
show 1 more comment
2 Answers
2
active
oldest
votes
Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal
to support in all cultures:
var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);
2
I thought a bit about it. That should be.ThenBy(p=>p, StringComparer.Ordinal)
in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania)"lt-LT"
you would have"Y"
before"R"
(the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic)"cs-CZ"
we get"DI"
before"CH"
since CH is a digraph in Czech and comes after H in its collation. Test for yourself withStringComparer.Create(new CultureInfo("lt-LT"), false)
etc.
– Jeppe Stig Nielsen
Dec 28 '15 at 8:54
add a comment |
First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").
var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
var sorted = columns.OrderBy(c => c.Length)
.ThenBy(c => c);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34488997%2fsort-liststring-like-excel-columns-sort%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal
to support in all cultures:
var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);
2
I thought a bit about it. That should be.ThenBy(p=>p, StringComparer.Ordinal)
in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania)"lt-LT"
you would have"Y"
before"R"
(the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic)"cs-CZ"
we get"DI"
before"CH"
since CH is a digraph in Czech and comes after H in its collation. Test for yourself withStringComparer.Create(new CultureInfo("lt-LT"), false)
etc.
– Jeppe Stig Nielsen
Dec 28 '15 at 8:54
add a comment |
Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal
to support in all cultures:
var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);
2
I thought a bit about it. That should be.ThenBy(p=>p, StringComparer.Ordinal)
in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania)"lt-LT"
you would have"Y"
before"R"
(the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic)"cs-CZ"
we get"DI"
before"CH"
since CH is a digraph in Czech and comes after H in its collation. Test for yourself withStringComparer.Create(new CultureInfo("lt-LT"), false)
etc.
– Jeppe Stig Nielsen
Dec 28 '15 at 8:54
add a comment |
Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal
to support in all cultures:
var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);
Update: Thanks @JeppeStigNielsen for correct comment, I add StringComparer.Ordinal
to support in all cultures:
var result = List.OrderBy(p=>p.Length).ThenBy(p=>p,StringComparer.Ordinal);
edited Dec 28 '15 at 10:57
answered Dec 28 '15 at 7:00
Reza ArabQaeniReza ArabQaeni
4,1942038
4,1942038
2
I thought a bit about it. That should be.ThenBy(p=>p, StringComparer.Ordinal)
in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania)"lt-LT"
you would have"Y"
before"R"
(the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic)"cs-CZ"
we get"DI"
before"CH"
since CH is a digraph in Czech and comes after H in its collation. Test for yourself withStringComparer.Create(new CultureInfo("lt-LT"), false)
etc.
– Jeppe Stig Nielsen
Dec 28 '15 at 8:54
add a comment |
2
I thought a bit about it. That should be.ThenBy(p=>p, StringComparer.Ordinal)
in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania)"lt-LT"
you would have"Y"
before"R"
(the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic)"cs-CZ"
we get"DI"
before"CH"
since CH is a digraph in Czech and comes after H in its collation. Test for yourself withStringComparer.Create(new CultureInfo("lt-LT"), false)
etc.
– Jeppe Stig Nielsen
Dec 28 '15 at 8:54
2
2
I thought a bit about it. That should be
.ThenBy(p=>p, StringComparer.Ordinal)
in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT"
you would have "Y"
before "R"
(the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ"
we get "DI"
before "CH"
since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false)
etc.– Jeppe Stig Nielsen
Dec 28 '15 at 8:54
I thought a bit about it. That should be
.ThenBy(p=>p, StringComparer.Ordinal)
in the end. Otherwise it will depend on the current culture in a complex way. For example with Lithuanian (Lithuania) "lt-LT"
you would have "Y"
before "R"
(the letter Y is collated between I and J in Lithuanian). Another example, in Czech (Czech Republic) "cs-CZ"
we get "DI"
before "CH"
since CH is a digraph in Czech and comes after H in its collation. Test for yourself with StringComparer.Create(new CultureInfo("lt-LT"), false)
etc.– Jeppe Stig Nielsen
Dec 28 '15 at 8:54
add a comment |
First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").
var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
var sorted = columns.OrderBy(c => c.Length)
.ThenBy(c => c);
add a comment |
First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").
var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
var sorted = columns.OrderBy(c => c.Length)
.ThenBy(c => c);
add a comment |
First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").
var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
var sorted = columns.OrderBy(c => c.Length)
.ThenBy(c => c);
First order by the length of column name ("C" comes before "AB"), then use normal alphabetical (string) sorting on strings with same length ("AC" before "AD").
var columns = new "A", "C", "AB", "AD", "N", "Z", "AC" ;
var sorted = columns.OrderBy(c => c.Length)
.ThenBy(c => c);
answered Dec 28 '15 at 7:01
Arghya CArghya C
6,24012342
6,24012342
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34488997%2fsort-liststring-like-excel-columns-sort%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If I were in your place I would have tried to sum the ascii code of the chars available and then sort it. For example A = 65, C = 67, ... AB = 65 + 66 = 131 and so on.
– Mohit Shrivastava
Dec 28 '15 at 7:05
3
Just to note: This order is called quasi-lexicographic order.
– Joey
Dec 28 '15 at 7:10
If you target .NET 4.5 (2012) or later, even if you tag this C# 4.0, and if you want to
Sort
theList<>
in-place, you could use something likeyourStringList.Sort(Comparer<string>.Create((x, y) => var c = x.Length.CompareTo(y.Length); if (c != 0) return c; return x.CompareTo(y); ));
. If the list may containnull
values, you need to take care of that in the lambda of course.– Jeppe Stig Nielsen
Dec 28 '15 at 7:30
@MohitShrivastava But that would make
"AB"
and"BA"
equal.– Jeppe Stig Nielsen
Dec 28 '15 at 7:33
Absolutely Right @JeppeStigNielsen.
.OrderBy(p=>p.Length).ThenBy(p=>p);
is the best way to do so. :)– Mohit Shrivastava
Dec 28 '15 at 7:34