Sort a datatable by col?

  • Thread starter Thread starter VM
  • Start date Start date
V

VM

How can I sort a datatable by a certain col after the table is filled?

Also, in my Windows datagrid, when I click on a column header that contains
integers 1-11, instead of sorting them correctly, it sorts them as 1,10,11,
2,3,... Windows Explorer does the same thing when sorting filenames that
begin w/ numbers. How can I fix this? The column being sorted is of type
Int.

Thanks and sorry for posting so many questions...
 
VM said:
How can I sort a datatable by a certain col after the table is filled?
The way you can do that is sticking each column into an array.
Then use a sort algorithm and sort all the arrays together
using the order defining column in the swap function. If
you need a sort class let me know.
 
Patrick de Ridder said:
The way you can do that is sticking each column into an array.
Then use a sort algorithm and sort all the arrays together
using the order defining column in the swap function. If
you need a sort class let me know.

You have to do the sorting before you bind the data to the table.
If there are three columns:
Take a,b,c bigger than necessary and later knock off the zeros
because of the n/2. I used static variables for a,b,c.

using System;

namespace xyz
{
public class Sort
{
private int [ ] a = new int[50];
private int [ ] b = new int[50];
private int [ ] c = new int[50];
private int lo;
private int hi;

public Sort(int [ ] a,int [ ] b,int [ ] c,int lo, int hi)
{
this.a = a;
this.b = b;
this.c = c;
this.hi = hi;
this.lo = lo;
quicksort(a,b,c,lo,hi);
}
public void quicksort(int [ ] a, int [ ] b, int [ ] c, int lo, int hi)
{
int i = lo;
int j = hi;
int h1 = 0;
int h2 = 0;
int h3 = 0;
int x = a[(lo+hi)/2];
do
{
while(a<x) i++;
while(a[j]>x) j--;
if(i<=j)
{
h1=a;a=a[j];a[j]=h1;
h2=b;b=b[j];b[j]=h2;
h3=c;c=c[j];c[j]=h3;
i++;
j--;
}
}while(i<=j);
if (lo<j) quicksort(a,b,c,lo,j);
if (i<hi)quicksort(a,b,c,i,hi);
}
}
}
 
Back
Top