此功能在一个数组上完成时效果很好:
var stringArray = ['48AB1','48AB10','48AB101','48AB106','48AB108','48AB11','48AB111','48AB1117','48AB1138','48AB18','48AB2'];
var regex = /^([a-z]*)(\d*)/i;
function sortFn(a, b) {
var _a = a.match(regex);
var _b = b.match(regex);
// if the alphabetic part of a is less than that of b => -1
if (_a[1] < _b[1]) return -1;
// if the alphabetic part of a is greater than that of b => 1
if (_a[1] > _b[1]) return 1;
// if the alphabetic parts are equal, check the number parts
var _n = parseInt(_a[2]) - parseInt(_b[2]);
if(_n == 0) // if the number parts are equal start a recursive test on the rest
return sortFn(a.substr(_a[0].length), b.substr(_b[0].length));
// else, just sort using the numbers parts
return _n;
}
console.log(stringArray.sort(sortFn))
我有一个由多列组成的数组,但是我只想在第一列上对该数组进行排序。我如何修改此代码以仅将一列推入排序功能?我从SQL服务中提取数据,并使用它来填充JQuery表。默认情况下,数据以自然顺序排序(即48AB1、48AB10、48AB100等)
我是编程的新手,因此将不胜感激。谢谢!
编辑: 这是有关如何创建所有内容的更多信息
var attrs = []
const activate = () => {
WyoArch.serverApi("QuerySites").then(sites => {
// jqxGrid creation
// --> reusable options to stylize grid
let mixinGridOptions = mixin => new Object({},
columns: mixin.columns,
source: createJqxDataAdapter(mixin.source || [])
})
attrs = sites;
console.log(attrs)
// --> sites
pickers.site.jqxGrid = $("#curateExisting-sitePicker .picker-jqxGrid").jqxGrid(mixinGridOptions({
source: sites,
columns: [{
text: 'ID',
datafield: 'SiteID',
width: 100
},
{
text: 'Name',
datafield: 'SiteName',
width: 150
},
{
text: 'Landowner',
datafield: 'LandOwner',
width: 100
},
{
text: 'TWN',
datafield: 'TWN',
width: 80
},
{
text: 'RNG',
datafield: 'RNG',
width: 80
},
{
text: 'Section',
datafield: 'Section',
width: 80
},
],
}))
}
}
答案 0 :(得分:0)
注意:此处的注释和问题明确了要求后,旧版本的废弃了。
在研究完问题并对其进行了更新之后,看来这将按照要求进行。数据是从图片中摘录的-请张贴文字,而不是文字图片! -可能与发生的情况不太匹配。它使用的功能与原始功能相同,但有所简化。
但是要点是包装sortFn
以便它可以在SiteID
字段中具有sort属性的元素上工作,您可以编写一个新的比较器,如下所示:
(a, b) => sortFn(a.SiteID, b.SiteID)
...并将其传递给table.sort()
。
const regex = /^([a-z]*)(\d*)/i;
const sortFn = (a, b) => {
if (a == '' && b == '') {return 0;}
const [a0, a1, a2] = a.match(regex);
const [b0, b1, b2] = b.match(regex);
return a1 < b1 ? -1 : a1 > b1 ? 1 : a2 - b2 || sortFn(a.substr(a0.length), b.substr(b0.length))
}
const sortBySiteId = (a, b) => sortFn(a.SiteID, b.SiteID)
var table = [
{SiteID: '1ABCTest', SiteName: 'App Testing RK', ContactID: 250},
{SiteID: '1ABCTest', SiteName: 'App Testing', ContactID: 250},
{SiteID: '1Albany', SiteName: 'RK Test', ContactID: 56},
{SiteID: '1BCDTest', SiteName: 'RK Test 2', ContactID: 201},
{SiteID: '1OFF', SiteName: 'One OFF', ContactID: 161},
{SiteID: '24BH406', SiteName: 'KOBOLD RANCH', ContactID: 250},
{SiteID: '42IN124', SiteName: null, ContactID: null},
{SiteID: '42IN40', SiteName: null, ContactID: null},
{SiteID: '42SV5', SiteName: null, ContactID: null},
{SiteID: '42UN95', SiteName: null, ContactID: null},
{SiteID: '48AB1', SiteName: 'CHINA WALL', ContactID: null},
{SiteID: '48AB10', SiteName: 'EL PRIMERO', ContactID: null},
];
console.log(table.sort(sortBySiteId))