Sort Alphanumeric on Array of more than one column












0















This function works great when done on one array:






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))





I have an array of multiple columns, but i only want to sort the array on the 1st column. How can i modify this code to push just the one column into the sort function? I am pulling the data from an SQL service and using it to populate a JQuery table. The data comes sorted by default in natural order (i.e. 48AB1,48AB10,48AB100, etc.)



I am novice at programming so any help would be appreciated. Thanks!



edit:
Here is more info on how I am creating it all






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










share|improve this question

























  • Suppose you have const 2dStringArray = [stringArray], simply 2dStringArray[0].sort(sortFn)

    – kemicofa
    Nov 15 '18 at 15:45













  • Can you supply a relatively short set of inputs and the expected output?

    – Scott Sauyet
    Nov 15 '18 at 15:50













  • I need to fill in a table that has 6 columns. The expected output would be that the table has the new sort applied by default. It would look like how the "Run Code Snippet" above would look

    – MrKirkwood
    Nov 15 '18 at 16:11













  • That snippet is a single-dimension array. It's not clear enough what your six columns means. (Does it look anything like the example in my answer?)

    – Scott Sauyet
    Nov 15 '18 at 16:22











  • I'm afraid the additional code doesn't help much. Are the values from the first part ('48AB1','48AB10', etc.) somehow tied to those from the second part({text: 'ID', datafield: 'SiteID', width: 100}, etc.)? You wanted to sort on a particular column. I try to answer that below, but only with a random-ish guess on what the data looks like and how that column is integrated with the rest. Your additions don't really describe that.

    – Scott Sauyet
    Nov 15 '18 at 16:36
















0















This function works great when done on one array:






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))





I have an array of multiple columns, but i only want to sort the array on the 1st column. How can i modify this code to push just the one column into the sort function? I am pulling the data from an SQL service and using it to populate a JQuery table. The data comes sorted by default in natural order (i.e. 48AB1,48AB10,48AB100, etc.)



I am novice at programming so any help would be appreciated. Thanks!



edit:
Here is more info on how I am creating it all






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










share|improve this question

























  • Suppose you have const 2dStringArray = [stringArray], simply 2dStringArray[0].sort(sortFn)

    – kemicofa
    Nov 15 '18 at 15:45













  • Can you supply a relatively short set of inputs and the expected output?

    – Scott Sauyet
    Nov 15 '18 at 15:50













  • I need to fill in a table that has 6 columns. The expected output would be that the table has the new sort applied by default. It would look like how the "Run Code Snippet" above would look

    – MrKirkwood
    Nov 15 '18 at 16:11













  • That snippet is a single-dimension array. It's not clear enough what your six columns means. (Does it look anything like the example in my answer?)

    – Scott Sauyet
    Nov 15 '18 at 16:22











  • I'm afraid the additional code doesn't help much. Are the values from the first part ('48AB1','48AB10', etc.) somehow tied to those from the second part({text: 'ID', datafield: 'SiteID', width: 100}, etc.)? You wanted to sort on a particular column. I try to answer that below, but only with a random-ish guess on what the data looks like and how that column is integrated with the rest. Your additions don't really describe that.

    – Scott Sauyet
    Nov 15 '18 at 16:36














0












0








0








This function works great when done on one array:






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))





I have an array of multiple columns, but i only want to sort the array on the 1st column. How can i modify this code to push just the one column into the sort function? I am pulling the data from an SQL service and using it to populate a JQuery table. The data comes sorted by default in natural order (i.e. 48AB1,48AB10,48AB100, etc.)



I am novice at programming so any help would be appreciated. Thanks!



edit:
Here is more info on how I am creating it all






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










share|improve this question
















This function works great when done on one array:






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))





I have an array of multiple columns, but i only want to sort the array on the 1st column. How can i modify this code to push just the one column into the sort function? I am pulling the data from an SQL service and using it to populate a JQuery table. The data comes sorted by default in natural order (i.e. 48AB1,48AB10,48AB100, etc.)



I am novice at programming so any help would be appreciated. Thanks!



edit:
Here is more info on how I am creating it all






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






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))





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))





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
},
],
}))
}
}





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
},
],
}))
}
}






javascript sorting alphanumeric






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:42







MrKirkwood

















asked Nov 15 '18 at 15:38









MrKirkwoodMrKirkwood

55




55













  • Suppose you have const 2dStringArray = [stringArray], simply 2dStringArray[0].sort(sortFn)

    – kemicofa
    Nov 15 '18 at 15:45













  • Can you supply a relatively short set of inputs and the expected output?

    – Scott Sauyet
    Nov 15 '18 at 15:50













  • I need to fill in a table that has 6 columns. The expected output would be that the table has the new sort applied by default. It would look like how the "Run Code Snippet" above would look

    – MrKirkwood
    Nov 15 '18 at 16:11













  • That snippet is a single-dimension array. It's not clear enough what your six columns means. (Does it look anything like the example in my answer?)

    – Scott Sauyet
    Nov 15 '18 at 16:22











  • I'm afraid the additional code doesn't help much. Are the values from the first part ('48AB1','48AB10', etc.) somehow tied to those from the second part({text: 'ID', datafield: 'SiteID', width: 100}, etc.)? You wanted to sort on a particular column. I try to answer that below, but only with a random-ish guess on what the data looks like and how that column is integrated with the rest. Your additions don't really describe that.

    – Scott Sauyet
    Nov 15 '18 at 16:36



















  • Suppose you have const 2dStringArray = [stringArray], simply 2dStringArray[0].sort(sortFn)

    – kemicofa
    Nov 15 '18 at 15:45













  • Can you supply a relatively short set of inputs and the expected output?

    – Scott Sauyet
    Nov 15 '18 at 15:50













  • I need to fill in a table that has 6 columns. The expected output would be that the table has the new sort applied by default. It would look like how the "Run Code Snippet" above would look

    – MrKirkwood
    Nov 15 '18 at 16:11













  • That snippet is a single-dimension array. It's not clear enough what your six columns means. (Does it look anything like the example in my answer?)

    – Scott Sauyet
    Nov 15 '18 at 16:22











  • I'm afraid the additional code doesn't help much. Are the values from the first part ('48AB1','48AB10', etc.) somehow tied to those from the second part({text: 'ID', datafield: 'SiteID', width: 100}, etc.)? You wanted to sort on a particular column. I try to answer that below, but only with a random-ish guess on what the data looks like and how that column is integrated with the rest. Your additions don't really describe that.

    – Scott Sauyet
    Nov 15 '18 at 16:36

















Suppose you have const 2dStringArray = [stringArray], simply 2dStringArray[0].sort(sortFn)

– kemicofa
Nov 15 '18 at 15:45







Suppose you have const 2dStringArray = [stringArray], simply 2dStringArray[0].sort(sortFn)

– kemicofa
Nov 15 '18 at 15:45















Can you supply a relatively short set of inputs and the expected output?

– Scott Sauyet
Nov 15 '18 at 15:50







Can you supply a relatively short set of inputs and the expected output?

– Scott Sauyet
Nov 15 '18 at 15:50















I need to fill in a table that has 6 columns. The expected output would be that the table has the new sort applied by default. It would look like how the "Run Code Snippet" above would look

– MrKirkwood
Nov 15 '18 at 16:11







I need to fill in a table that has 6 columns. The expected output would be that the table has the new sort applied by default. It would look like how the "Run Code Snippet" above would look

– MrKirkwood
Nov 15 '18 at 16:11















That snippet is a single-dimension array. It's not clear enough what your six columns means. (Does it look anything like the example in my answer?)

– Scott Sauyet
Nov 15 '18 at 16:22





That snippet is a single-dimension array. It's not clear enough what your six columns means. (Does it look anything like the example in my answer?)

– Scott Sauyet
Nov 15 '18 at 16:22













I'm afraid the additional code doesn't help much. Are the values from the first part ('48AB1','48AB10', etc.) somehow tied to those from the second part({text: 'ID', datafield: 'SiteID', width: 100}, etc.)? You wanted to sort on a particular column. I try to answer that below, but only with a random-ish guess on what the data looks like and how that column is integrated with the rest. Your additions don't really describe that.

– Scott Sauyet
Nov 15 '18 at 16:36





I'm afraid the additional code doesn't help much. Are the values from the first part ('48AB1','48AB10', etc.) somehow tied to those from the second part({text: 'ID', datafield: 'SiteID', width: 100}, etc.)? You wanted to sort on a particular column. I try to answer that below, but only with a random-ish guess on what the data looks like and how that column is integrated with the rest. Your additions don't really describe that.

– Scott Sauyet
Nov 15 '18 at 16:36












1 Answer
1






active

oldest

votes


















0














Note: older versions of this have been discarded after the comments here and in the question clarified the requirements.





After working through the question and updates to it, it seems like this will do what is requested. The data is snipped from an image -- please post text, not images of text! -- and might not quite match what's happening. This uses what is to me a function with the same behavior as the original, but somewhat simplified.



But the main point is that to wrap your sortFn so that it works on elements with the sort property in the SiteID field, you can just write a new comparator like this:



(a, b) => sortFn(a.SiteID, b.SiteID)


... and pass that to 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))








share|improve this answer


























  • I am getting an error when i try that "Uncaught TypeError: Cannot read property 'match' of undefined"

    – MrKirkwood
    Nov 15 '18 at 16:19











  • Are you getting that error in the console here (which would be strange as I don't) or in your own code?

    – Scott Sauyet
    Nov 15 '18 at 16:21











  • In my own code. Scott, I have updated the question above to show more of what i am doing. It is missing a lot of code but that is the guts.

    – MrKirkwood
    Nov 15 '18 at 16:25











  • I still get the "Uncaught TypeError: Cannot read property 'match' of undefined" error.

    – MrKirkwood
    Nov 15 '18 at 16:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322924%2fsort-alphanumeric-on-array-of-more-than-one-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Note: older versions of this have been discarded after the comments here and in the question clarified the requirements.





After working through the question and updates to it, it seems like this will do what is requested. The data is snipped from an image -- please post text, not images of text! -- and might not quite match what's happening. This uses what is to me a function with the same behavior as the original, but somewhat simplified.



But the main point is that to wrap your sortFn so that it works on elements with the sort property in the SiteID field, you can just write a new comparator like this:



(a, b) => sortFn(a.SiteID, b.SiteID)


... and pass that to 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))








share|improve this answer


























  • I am getting an error when i try that "Uncaught TypeError: Cannot read property 'match' of undefined"

    – MrKirkwood
    Nov 15 '18 at 16:19











  • Are you getting that error in the console here (which would be strange as I don't) or in your own code?

    – Scott Sauyet
    Nov 15 '18 at 16:21











  • In my own code. Scott, I have updated the question above to show more of what i am doing. It is missing a lot of code but that is the guts.

    – MrKirkwood
    Nov 15 '18 at 16:25











  • I still get the "Uncaught TypeError: Cannot read property 'match' of undefined" error.

    – MrKirkwood
    Nov 15 '18 at 16:53
















0














Note: older versions of this have been discarded after the comments here and in the question clarified the requirements.





After working through the question and updates to it, it seems like this will do what is requested. The data is snipped from an image -- please post text, not images of text! -- and might not quite match what's happening. This uses what is to me a function with the same behavior as the original, but somewhat simplified.



But the main point is that to wrap your sortFn so that it works on elements with the sort property in the SiteID field, you can just write a new comparator like this:



(a, b) => sortFn(a.SiteID, b.SiteID)


... and pass that to 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))








share|improve this answer


























  • I am getting an error when i try that "Uncaught TypeError: Cannot read property 'match' of undefined"

    – MrKirkwood
    Nov 15 '18 at 16:19











  • Are you getting that error in the console here (which would be strange as I don't) or in your own code?

    – Scott Sauyet
    Nov 15 '18 at 16:21











  • In my own code. Scott, I have updated the question above to show more of what i am doing. It is missing a lot of code but that is the guts.

    – MrKirkwood
    Nov 15 '18 at 16:25











  • I still get the "Uncaught TypeError: Cannot read property 'match' of undefined" error.

    – MrKirkwood
    Nov 15 '18 at 16:53














0












0








0







Note: older versions of this have been discarded after the comments here and in the question clarified the requirements.





After working through the question and updates to it, it seems like this will do what is requested. The data is snipped from an image -- please post text, not images of text! -- and might not quite match what's happening. This uses what is to me a function with the same behavior as the original, but somewhat simplified.



But the main point is that to wrap your sortFn so that it works on elements with the sort property in the SiteID field, you can just write a new comparator like this:



(a, b) => sortFn(a.SiteID, b.SiteID)


... and pass that to 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))








share|improve this answer















Note: older versions of this have been discarded after the comments here and in the question clarified the requirements.





After working through the question and updates to it, it seems like this will do what is requested. The data is snipped from an image -- please post text, not images of text! -- and might not quite match what's happening. This uses what is to me a function with the same behavior as the original, but somewhat simplified.



But the main point is that to wrap your sortFn so that it works on elements with the sort property in the SiteID field, you can just write a new comparator like this:



(a, b) => sortFn(a.SiteID, b.SiteID)


... and pass that to 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))








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))





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))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 18:56

























answered Nov 15 '18 at 16:09









Scott SauyetScott Sauyet

21.1k22757




21.1k22757













  • I am getting an error when i try that "Uncaught TypeError: Cannot read property 'match' of undefined"

    – MrKirkwood
    Nov 15 '18 at 16:19











  • Are you getting that error in the console here (which would be strange as I don't) or in your own code?

    – Scott Sauyet
    Nov 15 '18 at 16:21











  • In my own code. Scott, I have updated the question above to show more of what i am doing. It is missing a lot of code but that is the guts.

    – MrKirkwood
    Nov 15 '18 at 16:25











  • I still get the "Uncaught TypeError: Cannot read property 'match' of undefined" error.

    – MrKirkwood
    Nov 15 '18 at 16:53



















  • I am getting an error when i try that "Uncaught TypeError: Cannot read property 'match' of undefined"

    – MrKirkwood
    Nov 15 '18 at 16:19











  • Are you getting that error in the console here (which would be strange as I don't) or in your own code?

    – Scott Sauyet
    Nov 15 '18 at 16:21











  • In my own code. Scott, I have updated the question above to show more of what i am doing. It is missing a lot of code but that is the guts.

    – MrKirkwood
    Nov 15 '18 at 16:25











  • I still get the "Uncaught TypeError: Cannot read property 'match' of undefined" error.

    – MrKirkwood
    Nov 15 '18 at 16:53

















I am getting an error when i try that "Uncaught TypeError: Cannot read property 'match' of undefined"

– MrKirkwood
Nov 15 '18 at 16:19





I am getting an error when i try that "Uncaught TypeError: Cannot read property 'match' of undefined"

– MrKirkwood
Nov 15 '18 at 16:19













Are you getting that error in the console here (which would be strange as I don't) or in your own code?

– Scott Sauyet
Nov 15 '18 at 16:21





Are you getting that error in the console here (which would be strange as I don't) or in your own code?

– Scott Sauyet
Nov 15 '18 at 16:21













In my own code. Scott, I have updated the question above to show more of what i am doing. It is missing a lot of code but that is the guts.

– MrKirkwood
Nov 15 '18 at 16:25





In my own code. Scott, I have updated the question above to show more of what i am doing. It is missing a lot of code but that is the guts.

– MrKirkwood
Nov 15 '18 at 16:25













I still get the "Uncaught TypeError: Cannot read property 'match' of undefined" error.

– MrKirkwood
Nov 15 '18 at 16:53





I still get the "Uncaught TypeError: Cannot read property 'match' of undefined" error.

– MrKirkwood
Nov 15 '18 at 16:53




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322924%2fsort-alphanumeric-on-array-of-more-than-one-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."