javascript网格按数字排序,排序问题

标签 javascript sorting grid

我有一个 JS 排序脚本,但排序 # 是这样的:

1.
10.
11.
2.
3.
4.

我希望它们按值排序,如下所示:

1.
2.
3.
4.
10.
11.

我想不通。我知道有一种方法可以用 MySQL 做到这一点,但我不熟悉 javascript。到目前为止,这是我所拥有的,但它订购的数字不正确。感谢您提供的任何帮助,或者为我指明正确的方向。

JavaScript:

var Grid = new Class({
    /** define some variables */
    table: false, 
    headers: false, 
    data: false, 

    /**
     * Initialize the object
     */
    initialize: function(table){
        this.table = table;
        this.getHeaders();
    }, 

    /**
     * Get the headers
     */
    getHeaders: function(){
        this.headers = this.table.getElements('thead tr th');
        this.headers.each(function(h, index){
            //h.store('asc', false);
            if (h.hasClass('sort')) h.addEvent('click', function(){
                if(h.hasClass('asc')){
                    h.className = 'sort desc';

                    //h.addClassName('desc');
                }else{
                    h.className = 'sort asc';
                    //h.addClassName('asc');
                }
                //if (h.retrieve('asc')) h.store('asc', false);
                //else h.store('asc', true);
                //this.sort($('tblrank_id'));
                this.sort(index);
            }.bind(this));
        }, this);
    }, 

    /**
     * Get the table data
     */
    getData: function(){
        this.data = this.table.getElements('tbody tr');
    }, 

    /**
     * Sort the data
     * @param int index
     */
    sort: function(index){
        this.getData();
        data = [];
        sortType = this.headers[index].getProperty('axis');

        asc = this.headers[index].hasClass('asc');//this.headers[index].retrieve('asc');
        if (this.data.length > 0) this.data.each(function(row, i){
            cells = row.getElements('td');
            if (cells.length < this.headers.length) return false;
            value = cells[index].innerHTML;

            if(cells[index].childNodes[0].nodeName.toLowerCase() == "a"){
                //alert(cells[index].childNodes[0].innerHTML);
                value = cells[index].childNodes[0].innerHTML;
            }

            if (sortType == 'int' || sortType == 'float'){
                if (value.contains('$') || value.contains(',')) value = value.replace(/\$/g, '').replace(/,/g, '').toFloat();
                else value = value.toFloat();
            } else if (sortType == 'date') value = Date.parse(value);
            data.push({'index': i, 'value': value, 'row': row});
        }, this);

        if (sortType == 'int' || sortType == 'float' || sortType == 'date') data.sort(this.sortNumeric);
        else data.sort(this.sortCaseInsensitive);
        if (!asc) data.reverse();

        this.data = [];
        data.each(function(d, i){
            this.data.push(d.row);
        }, this);

        this.data.each(function(row, i){
            if (row.hasClass('etblraw0')) row.removeClass('etblraw0');
            if (row.hasClass('etblraw1')) row.removeClass('etblraw1');
            this.table.getElement('tbody').adopt(row.addClass((i % 2 == 0 ? 'etblraw0' : 'etblraw1')));


        }, this);
    }, 

    /**
     * Sort Numerica Values
     * @param object a
     * @param object b
     */
    sortNumeric: function(a, b){


        if ($type(a.value) != 'number') a.value = 0;
        if ($type(b.value) != 'number') b.value = 0;
        return a.value - b.value;
    }, 

    sortCaseInsensitive: function(a, b){
        a.value = a.value.toLowerCase();


        b.value = b.value.toLowerCase();
        if (a.value == b.value) return 0;
        if (a.value < b.value) return -1;
        return 1;
    }
);

最佳答案

试试这个:

function sortfunc(a,b) {
  return parseInt(a.split(".")[0]) -
         parseInt(b.split(".")[0]);
}

var list = ["1. first", "2. second", "10. tenth"];
list.sort(sortfunc);

console.log(list); // => ["1. first", "2. second", "10. tenth"]

关于javascript网格按数字排序,排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10391880/

相关文章:

javascript - 在单击按钮的位置定位下拉菜单

html - CSS 列无法正常工作

css - 如何在较小的屏幕上将一行 6 个图像变成 2 行 3 个图像

javascript - 如何获取变量的值?

javascript - 使用 jQuery 在现有 HTML 元素周围包装 HTML 结构

arrays - 二维数组排序问题

Postgresql - 不正确的排序

css - <img> 的 html5 垂直间距问题

javascript - 尝试分配给只读属性,即使相同的命令在其他地方也有效?

python - python 的 sorted() 使用什么算法?