I have question similar to this one JS array sort with regex but I have array of objects like this:
[
{
"applicationName":"APR",
"homepage":"https://apr.apache.org/",
"version":"1.5.2",
"toolchain":"goolf-1.7.20",
"description":"The Apache Portable Runtime (APR) is a supporting library for the Apache web server.\nIt provides a set of application programming interfaces (APIs) that map to the underlying Operating System (OS). Where the OS doesn't support a particular function, APR will provide an emulation. Thus programmers can use the APR to make a program portable across different platforms.",
"$$hashKey":"object:117"
},
{
"applicationName":"APR-util",
"homepage":"https://apr.apache.org/",
"version":"1.5.4",
"toolchain":"goolf-1.7.20",
"description":"The Apache Portable Runtime Utility Library provides a predictable \nand consistent interface to underlying client library interfaces. This application programming interface assures predictable if not identical behaviour regardless of which libraries are available on a given platform.",
"$$hashKey":"object:118"
},
{
"applicationName":"ATK",
"homepage":"https://developer.gnome.org/ATK/stable/",
"version":"2.16.0",
"toolchain":"goolf-1.7.20",
"description":"\n ATK provides the set of accessibility interfaces that are implemented by other\n toolkits and applications. Using the ATK interfaces, accessibility tools have\n full access to view and control running applications.",
"$$hashKey":"object:119"
},
{
"applicationName":"ATLAS",
"homepage":"http://math-atlas.sourceforge.net",
"version":"3.8.4",
"toolchain":"gompi-1.7.20-with-shared-libs",
"description":"ATLAS (Automatically Tuned Linear Algebra Software) is the application of\n the AEOS (Automated Empirical Optimization of Software) paradigm, with the present emphasis \n on the Basic Linear Algebra Subprograms (BLAS), a widely used, performance-critical, linear \n algebra kernel library.",
"$$hashKey":"object:120"
},
{
"applicationName":"ActiveTcl",
"homepage":"http://www.activestate.com/activetcl",
"version":"8.5.18.0",
"toolchain":"dummy-dummy",
"description":"ActiveTcl is the leading commercial-grade distribution of the open source Tcl programming language.",
"$$hashKey":"object:121"
},
{
"applicationName":"ActiveTcl",
"homepage":"http://savannah.gnu.org/projects/parallel/",
"version":"8.6.4.1",
"toolchain":"dummy-dummy",
"description":"ActiveTcl"
},
{
"applicationName":"Anaconda2",
"homepage":"https://www.continuum.io/why-anaconda",
"version":"2.4.0",
"toolchain":"dummy-dummy",
"description":"Anaconda is a completely free Python 2 distribution (including for commercial use and redistribution). It includes more than 300 of the most popular Python packages for science, math, engineering, and data analysis."
},
{
"applicationName":"Anaconda2",
"homepage":"https://www.continuum.io/why-anaconda",
"version":"2.5.0",
"toolchain":"dummy-dummy",
"description":"Anaconda is a completely free Python 2 distribution (including for commercial use and redistribution). It includes more than 300 of the most popular Python packages for science, math, engineering, and data analysis."
},
{
"applicationName":"Anaconda3",
"homepage":"https://www.continuum.io/why-anaconda",
"version":"2.3.0",
"toolchain":"dummy-dummy",
"description":"Anaconda is a completely free Python 3 distribution (including for commercial use and redistribution). It includes more than 300 of the most popular Python packages for science, math, engineering, and data analysis."
},
{
"applicationName":"Anaconda3",
"homepage":"https://www.continuum.io/why-anaconda",
"version":"2.4.0",
"toolchain":"dummy-dummy",
"description":"Anaconda is a completely free Python 3 distribution (including for commercial use and redistribution). It includes more than 300 of the most popular Python packages for science, math, engineering, and data analysis."
}
]
and I need to sort it based on regex taken from user and it should show first applications that start with the text provided by user then it should show application regex match description and toolchain, here is my code:
export default class CustomTabsShowController {
static $inject = ['$stateParams', '$state', 'customTabRepository'];
constructor($stateParams, $state, customTabRepository) {
this.searchTerm = "";
this.id = $stateParams.id;
this.$state = $state;
this.customTabRepository = customTabRepository;
}
getSortRank(obj) {
var regex;
var array = [obj.applicationName, obj.description, obj.toolchain];
if (!this.searchTerm.match(/^\^/)) {
regex = new RegExp('^' + this.searchTerm);
for (var i = 0; i < array.length; i++) {
if (regex.test(array[i])) {
console.log(array[i] + ' ' + i);
return i;
}
}
}
regex = new RegExp(this.searchTerm);
for (var i = 0; i < array.length; i++) {
if (regex.test(array[i])) {
console.log(array[i] + ' ' + (i + 3));
return i + 3;
}
}
return array.length + 3;
}
search() {
this.tableParams.filter({ $: this.searchTerm });
this.customTabRepository.data.sort((a,b) => {
return this.getSortRank(a) - this.getSortRank(b);
});
}
}
I'm sorting angular ng-table data but that should not be important. My code don't work as expected, it don't sort right, what wrong with my code?
My code don't work as expected, it don't sort right, what wrong with my code?what is wrong with it?getSortedRankreturns the same value for both, and it might beundefinedor something.