对多列数组的字母数字进行排序
Sort Alphanumeric on Array of more than one column
这个函数在一个数组上完成时效果很好:
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 table。默认情况下,数据按自然顺序排序(即 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
},
],
}))
}
}
Screenshot of teh console log of the array
注意:在此处的评论和问题中阐明要求后,旧版本已被丢弃。
在解决问题并对其进行更新后,这似乎可以满足要求。数据是从图像中截取的——请 post 文本,而不是文本图像! - 并且可能与正在发生的事情不完全匹配。这使用对我来说是一个与原始行为相同但有所简化的函数。
但要点是包装你的 sortFn
以便它适用于 SiteID
字段中排序为 属性 的元素,你可以编写一个新的比较器,例如这个:
(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))
这个函数在一个数组上完成时效果很好:
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 table。默认情况下,数据按自然顺序排序(即 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
},
],
}))
}
}
Screenshot of teh console log of the array
注意:在此处的评论和问题中阐明要求后,旧版本已被丢弃。
在解决问题并对其进行更新后,这似乎可以满足要求。数据是从图像中截取的——请 post 文本,而不是文本图像! - 并且可能与正在发生的事情不完全匹配。这使用对我来说是一个与原始行为相同但有所简化的函数。
但要点是包装你的 sortFn
以便它适用于 SiteID
字段中排序为 属性 的元素,你可以编写一个新的比较器,例如这个:
(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))