Oracle同一列中存放数字或字母时的排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle同一列中存放数字或字母时的排序相关的知识,希望对你有一定的参考价值。
表A的字符串b列中存放以下数据
·1,6,3,g,8,a,22,h,10,15,
希望得到的排序后顺序
1,3,6,8,10,15,22,a,g,h
由于是字符串所以asc时10会排到3的前面。
想到的一个方法是把b列转成数字a列(字母的话转成null),之后a asc ,b asc
求其他比较简单的方法
之假设你的数据在B栏,标题栏,然后找到一个空列(如C列),在C2中输入
= 1 *权利(B2,4)根据
拖动填充。
在这一点上,C系列已经实现年
选择数据区域,你可以列进行排序用C本回答被提问者采纳
表仅按字母顺序排序,不是数字排序[关闭]
我有一个由PHP变量填充的表。 PHP值来自API的JSON。
我想在同一个表格中按字母和数字排序。
以下功能有效,但只能按字母顺序对其进行排序。
我可以修改此功能以进行数字排序吗?
<script>
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
switching = true;
//Set the sorting direction to ascending:
dir = "asc";
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/*If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again.*/
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
</script>
答案
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
var regex = /$|USD|EURO|,/gi;
var xinnerHTML="", yinnerHTML="";
table = document.getElementById("myTable");
switching = true;
dir = "asc";
while (switching) {
switching = false;
rows = table.getElementsByTagName("TR");
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
xinnerHTML = x.innerHTML.replace(regex, '');
yinnerHTML = y.innerHTML.replace(regex, '');
var xContent = (isNaN(xinnerHTML))
? (xinnerHTML.toLowerCase() === '-')
? 0 : xinnerHTML.toLowerCase()
: parseFloat(xinnerHTML);
var yContent = (isNaN(yinnerHTML))
? (yinnerHTML.toLowerCase() === '-')
? 0 : yinnerHTML.toLowerCase()
: parseFloat(yinnerHTML);
if (dir == "asc") {
if (xContent > yContent) {
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (xContent < yContent) {
shouldSwitch= true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount ++;
} else {
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
另一答案
我改变了方向,找到了有效的新代码。但是,如果同一单元格中有数字和字母,则不会按数字排序。我该怎么做?我将在下面粘贴新代码。
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
switching = true;
dir = "asc";
while (switching) {
switching = false;
rows = table.getElementsByTagName("TR");
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
var xContent = (isNaN(x.innerHTML))
? (x.innerHTML.toLowerCase() === '-')
? 0 : x.innerHTML.toLowerCase()
: parseFloat(x.innerHTML);
var yContent = (isNaN(y.innerHTML))
? (y.innerHTML.toLowerCase() === '-')
? 0 : y.innerHTML.toLowerCase()
: parseFloat(y.innerHTML);
if (dir == "asc") {
if (xContent > yContent) {
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (xContent < yContent) {
shouldSwitch= true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount ++;
} else {
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
以上是关于Oracle同一列中存放数字或字母时的排序的主要内容,如果未能解决你的问题,请参考以下文章