Sorting a Datatable

G

Guest

Greetings,

I am trying to sort this values on a datatable

1
2
12rw
3
12r
4v
10a
e1
1w3

This was suposed to be sorted like this

1
1w3
2
3
4v
10a
12r
12rw
e1

I can't find a solution to sort this way.

Can anyone help me?
 
G

Guest

You could parse each string to get the preceding numeric values until the
first character is found. Use the numeric values converted to a number to
sort. When the numbers match, use the characters that follow to make the
decision. Similarly, if the string starts with a character use that to sort.
You might want to write a method that does the comparison between two strings
the way you want and use that in a bubble sort function.

Adrian.
 
N

Nicholas Paldino [.NET/C# MVP]

Why would you use a bubble sort function for this, or any application,
for that matter? It takes O(N^2) time to do this, just way too long. There
are much faster sorting algorithms out there (quicksort, for example).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Adrian Voicu said:
You could parse each string to get the preceding numeric values until the
first character is found. Use the numeric values converted to a number to
sort. When the numbers match, use the characters that follow to make the
decision. Similarly, if the string starts with a character use that to
sort.
You might want to write a method that does the comparison between two
strings
the way you want and use that in a bubble sort function.

Adrian.
--
[Please mark my answer if it was helpful to you]




Diogo Alves said:
Greetings,

I am trying to sort this values on a datatable

1
2
12rw
3
12r
4v
10a
e1
1w3

This was suposed to be sorted like this

1
1w3
2
3
4v
10a
12r
12rw
e1

I can't find a solution to sort this way.

Can anyone help me?
 
G

Guest

I don't know, it's the first thing that came to mind :) Obviously you can do
it in many other ways as long as you understand where to start.

Adrian.
--
[Please mark my answer if it was helpful to you]




Nicholas Paldino said:
Why would you use a bubble sort function for this, or any application,
for that matter? It takes O(N^2) time to do this, just way too long. There
are much faster sorting algorithms out there (quicksort, for example).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Adrian Voicu said:
You could parse each string to get the preceding numeric values until the
first character is found. Use the numeric values converted to a number to
sort. When the numbers match, use the characters that follow to make the
decision. Similarly, if the string starts with a character use that to
sort.
You might want to write a method that does the comparison between two
strings
the way you want and use that in a bubble sort function.

Adrian.
--
[Please mark my answer if it was helpful to you]




Diogo Alves said:
Greetings,

I am trying to sort this values on a datatable

1
2
12rw
3
12r
4v
10a
e1
1w3

This was suposed to be sorted like this

1
1w3
2
3
4v
10a
12r
12rw
e1

I can't find a solution to sort this way.

Can anyone help me?
 
R

Rick Lones

Diogo said:
Greetings,

I am trying to sort this values on a datatable

1
2
12rw
3
12r
4v
10a
e1
1w3

This was suposed to be sorted like this

1
1w3
2
3
4v
10a
12r
12rw
e1

I can't find a solution to sort this way.

Can anyone help me?

Assuming these "values" are strings, you will have to define the custom rule by
which they are to be ordered. You don't show the actual result you obtained,
but the default would be lexicographic order, which would come out as:

1, 10a, 12r, 12rw, 2, 3, 4v, e1

One way to define your own order would be to define a class which wraps each
string in an class instance, and then implement IComparable on the class. This
may not be easy in your case at hand. Your desired ordering is not (to me, at
least) intuitive. I would have guessed that "e1" would come first, not last,
for example, given the rest of your apparent pattern, which appears to be
something like "numeric part followed by alphanumeric part".

Basically it comes down to whether you can implement IComparable in a way which
gives the correct relative order of any two items in all cases. If not, then
your sort order is not well enough defined. It is tough to tell from your example.

HTH,
-rick-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top