how to check if list of Items which are in alphanumeric shape are sorted asc and in desc order using c#

satmisha
satmisha used Ask the Experts™
on
I have two lists of alphanumerics provided below, How could I ensure that they are sorted based on our regular expression i.e. [Z-A, 9-0] and [0-9, A-Z] . In list two blank should come first as mentioned in List-2.

List-1
T
S
IApplicationCache
D
A
10
9
8
7

Open in new window


List-2
""
1
2
34
56
A
B

Z

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Your question is unclear. Are List1 and List2 both example of source data, or is one a "before" and the other an "after"?

Also, regular expressions are very literal constructs. Your pattern has a comma in it. In regex, a comma inside of braces would be evaluated as a potential character match. Is this what you intend, or is your pattern more pseudo-code?
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi satmisha;

Adding to @kaufmed statements  this, " [Z-A, 9-0]", is also an invalid regex pattern seeming that the range is in reverse order. Also in sorting digit characters will not place them in numerical order. For example if you had the strings, "1", "4", "9", "12",  and it's  ascending sort order would result as , "1", "12", "4", "9".

Author

Commented:
Apologies for non-clarity and thanks for replying. Answering your questions.

# Yes, Both List1 and List2 are examples of source data
# It is more of a scedo code indeed, [Z-A, 9-0] indicated reverse alphabetical order and [0-9, A-Z] indicates  in alphabetical order.
#  [Z-A, 9-0] is a scudo code indicating that it is in reverse alphabetic order.

I hope this answers your doubts.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
apart from that during my research I stumbled upon one article i.e. https://www.dotnetperls.com/issorted which does make sence but it appears that it does not work well with alphanumeric string.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
In the link to the article "The SortTools" you posted what do you mean, "but it appears that it does not work well with alphanumeric string", is that due to the issue I stated in my last post?

Author

Commented:
Thank you, Fernando.

Providing below details, hope this will clear the question in more details...now..

I am using the Datatable jquery plugin to sort the table which sorts well but when I compared the output of the same input with the c# code I found it is different. I have no idea why is it so and which sort is right. Enclosing runable code for c# with output, and the output of the DataTable js plugin sort.

using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		List<string> colors = new List<string>
        {
           
"VW Transporter (AMB Equipement)",
"VW Transporter - OBD (AMB EQUIPEMENT)",
"Vauxhall Insignia - Unit 390622",
"Vauxhall Insignia (TCSV11 Rev G – battery mounted)"

        };
        // Call Array.Sort method.
        List<String> displayNamesSorted = new List<string>(colors);
                        displayNamesSorted.Sort();
                        displayNamesSorted.Reverse();
                        
		
		foreach (string color in displayNamesSorted)
        {
            Console.WriteLine(color);
        }
	}
}

Open in new window


Output:
VW Transporter (AMB Equipement)
VW Transporter - OBD (AMB EQUIPEMENT)
Vauxhall Insignia (TCSV11 Rev G – battery mounted)
Vauxhall Insignia - Unit 390622


whereas when I sort using DataTable.js plugin of Jquery it comes out:

VW Transporter - OBD (AMB EQUIPEMENT)
VW Transporter (AMB Equipement)
Vauxhall Insignia - Unit 390622
Vauxhall Insignia (TCSV11 Rev G – battery mounted)
Retired
Distinguished Expert 2017
Commented:
Hi satmisha;

In C# sorting is accomplished by where the character falls with in the ASCII code table. For example the character 'A' has a numeric value of 65 and the character 'a' has a numeric value of 97. When sorting 'A' comes before 'a' and also why numeric characters don't sort naturally. In your example for the C# version the character '(' has a numeric value of 40 while the character '-' has a value of 45 and the reason why '(' comes before '-'. It is apparent that the Jquery sort order for the DataTable is not the same as what used in this C# version. This does not make one sort algorithm more correct than another. I have not worked with the Jquery DataTable plug-in but I believe it has a way to provide your own sorthing algorithm to override the supplied one.

I hope that helped in answering your question.

Author

Commented:
Thank you Fernando Soto. Appreciate your time :-).
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem satmisha, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial