473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sort a datatable by col?

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...
Nov 16 '05 #1
2 1446
"VM" <vo******@yahoo.com> wrote in message
news:O8**************@TK2MSFTNGP11.phx.gbl...
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.
Nov 16 '05 #2

"Patrick de Ridder" <wa********@all.here> wrote in message
news:4a***************************@freeler.nl...
"VM" <vo******@yahoo.com> wrote in message
news:O8**************@TK2MSFTNGP11.phx.gbl...
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.


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[i]<x) i++;
while(a[j]>x) j--;
if(i<=j)
{
h1=a[i];a[i]=a[j];a[j]=h1;
h2=b[i];b[i]=b[j];b[j]=h2;
h3=c[i];c[i]=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);
}
}
}
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ETX | last post by:
Hello, My question is about the datagrid sorting. I have a datatable that is filled up with the data retrieved from an SQL query.I use that datatable as source for my datagrid. I would like to...
6
by: Hardy Wang | last post by:
Hi, I have a DataSet, returned by a stored procudure, which has only on DataTable. At this step there is no sort in DataTable. Is there is a way I can do to sort DataRows in this DataTable (not...
2
by: Fredrik Rodin | last post by:
All, I've been looking around for a solution to my problem for a couple of days now. In short, here's my situation: 1. I'm getting a result from a component back as a datatable and I have...
1
by: troyblakely | last post by:
I am having trouble sorting a datagrid. I have read numerous posts on this and other lists, and tried most of the suggestions, but none of them have worked for me yet. I populate a dataset from two...
5
by: Gene Hubert | last post by:
I'm using the DefaultView from the Datasource for a DataGrid to present the data in a particular order. It seems that sorting in this way is an "Active Sort", as is the default sort that is...
1
by: Lyners | last post by:
I am trying to figure out the best way to do this (currently I am having a problem sorting). I have a vb.net program that contains 2 datagrids on a form for the end user. When the user is ready to...
3
by: gilly3 | last post by:
I have a column in a DataView that contains NaN. When I attempt to Sort on this column, I get: MESSAGE: Index was outside the bounds of the array. SOURCE: System.Data STACKTRACE: at...
3
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Are there any good built-in or availble functions that I can call to sort my dataset datatalbe based on 1 or more of the table's columns? -- Thanks.
4
by: sklett | last post by:
(I posted this in a databinding NG, but it's a VERY low traffic NG so I thought I would post here as well. I hope no one minds too much, if you do I'm sorry) I have a DGV that is bound to a...
3
by: Bimal Kothari | last post by:
private DataTable SortDataTable(DataTable GetDataTable, string sort) { DataTable _NewDataTable = GetDataTable.Clone(); int rowCount = GetDataTable.Rows.Count; DataRow foundRows =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.