在Asp.net,C#,SqlServer中对浮点数进行排序 [英] Sorting Floating Number in Asp.net,C#,SqlServer

查看:49
本文介绍了在Asp.net,C#,SqlServer中对浮点数进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数字表(文本格式)的数据库.
我要排序

<br />
1.1<br />
1.2<br />
1.1.1<br />
1.2.1<br />
1.1.2<br />
1.2<br />
1.2.1<br />
1.22.11<br />
1.3<br />
1.4<br />
etc<br />



我希望它在整理后像这样出现:

<br />
1.1<br />
1.1.1<br />
1.1.2<br />
1.2<br />
1.2.1<br />
1.2.2<br />
1.22.11<br />
1.3<br />
1.4<br />
etc<br />



有可能吗?在此先谢谢您

解决方案

一种简单的方法是创建一个使用临时表的小程序,将字符串分成几部分(最有可能是数字),然后获取,对数据进行排序和连接,然后将结果集返回给客户端.该过程可以在T-SQL中完成,但是考虑到您将要使用字符串,因此使用C#创建该过程会更容易. br/> 请根据您的要求增强此代码.

//Code for creating table can be ignored if you are fetching data from DB.
string[] items = { "1.1", "1.2", "1.1.1", "1.2.1", "1.1.2", "1.2", "1.2.1", "1.22.11", "1.3", "1.4" };
DataTable dtItem = new DataTable();
dtItem.Columns.Add("tab_index", typeof(string));
foreach (string item in items)
{
    dtItem.Rows.Add(item);
}
dtItem.AcceptChanges();
//Now just assign this to DataView and sort the same, column has to be string
DataView dvItem = dtItem.DefaultView;
dvItem.Sort = "tab_index";

foreach (DataRowView viewItem in dvItem)
{
    System.Diagnostics.Debug.WriteLine(viewItem["tab_index"].ToString());
}


 选择列名 from  表格 订单 列名



编辑==========

那么,为什么要对我的回答投2分呢?没用吗?


I have a db with a table (text format) with numbers.
I want to sort it

<br />
1.1<br />
1.2<br />
1.1.1<br />
1.2.1<br />
1.1.2<br />
1.2<br />
1.2.1<br />
1.22.11<br />
1.3<br />
1.4<br />
etc<br />



I want it to come up like this after sorting:

<br />
1.1<br />
1.1.1<br />
1.1.2<br />
1.2<br />
1.2.1<br />
1.2.2<br />
1.22.11<br />
1.3<br />
1.4<br />
etc<br />



Is it possible and how? Thanks in advance

解决方案

One straight-forward way is to create a small procedure which would use temporary table, split the strings into pieces (numeric most likely) and then fetch, sort and concatenate the data and return the result set to the client. The procedure can be done in T-SQL but considering that you are going to play with strings, it would be easier to use C# to create the procedure.


This example might help you.
Please enhance this code as per your requirement.

//Code for creating table can be ignored if you are fetching data from DB.
string[] items = { "1.1", "1.2", "1.1.1", "1.2.1", "1.1.2", "1.2", "1.2.1", "1.22.11", "1.3", "1.4" };
DataTable dtItem = new DataTable();
dtItem.Columns.Add("tab_index", typeof(string));
foreach (string item in items)
{
    dtItem.Rows.Add(item);
}
dtItem.AcceptChanges();
//Now just assign this to DataView and sort the same, column has to be string
DataView dvItem = dtItem.DefaultView;
dvItem.Sort = "tab_index";

foreach (DataRowView viewItem in dvItem)
{
    System.Diagnostics.Debug.WriteLine(viewItem["tab_index"].ToString());
}


select columnname from table order by columnname



EDIT ==========

So why did you vote my answer a 2? Did it not work?


这篇关于在Asp.net,C#,SqlServer中对浮点数进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆