如何根据本地存储中的变量对某些内容进行快速排序?
How do I quicksort something based on its variable in local storage?
您好,我正在尝试通过其 ID 对 localStorage 中的一些数据进行排序。
数据的格式是这样的:
{"Brand":"Acura","Model":"Choose your model","Age":"1998","KM":"10","Name" :"Harry","ContactInfo":"123456677"}
我想按 KM 值从低到高的顺序排序。
我的本地存储密钥已注册,这是我正在使用的代码:
function Qsort() {
var mymain = JSON.parse(localStorage.getItem("register"));
var result = quicksort(mymain, 0, mymain.length - 1);
// the following line fo code shows the final result of the sorted numbers or strings
console.log(mymain);
console.log(quicksort(mymain, 0, mymain.length - 1));
return result;
}
function quicksort(mymain, left, right) {
var index;
// checks if there are more than one numbers
if (mymain.length > 1) {
// partition will find a pivot then split leist in two either left of right.
// Left list contains everything that is smaller than the pivot and the right contians everythign larger than pivot
index = partition(mymain, left, right);
// will treat left side aas a new problem and will then run the sort
if (left < index - 1){
quicksort(mymain, left, index - 1)
}
// will treat right side as a new problem and will then run the sort
if (index < right) {
quicksort(mymain, index, right)
}
}
return mymain
}
// Divides the whole function
function partition(mymain, left, right) {
var pivot = mymain[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (mymain[i] < pivot) {
i++;
}
while (mymain[j] > pivot) {
j--;
}
if (i <= j) {
swap(mymain, i, j);
i++;
j--;
}
}
return i;
}
// swaps the values based on how high or low the number is
function swap(mymain, firstIndex, secondIndex) {
var temp = mymain[firstIndex];
mymain[firstIndex] = mymain[secondIndex];
mymain[secondIndex] = temp;
}
我应该如何处理 var mymain
部分,以便它只获取 KM 下定义的值。
假设 localStorage
中 "register"
处的项目是一个数组,您可以简单地做...
var mymain = JSON.parse(localStorage.getItem("register"))
var justKMs = mymain.map(function(data){ return data.KM; });
但这会 return 一个仅包含 KM 值的数组,因此您最终将不得不搜索 mymain
列表以获取其余数据,我假设是不是你想要的。
另一种方法是添加一个 "lookup" 函数来告诉快速排序 "how" 获取要用于排序的值。让我们将查找函数签名简单地定义为 function lookup( item ) => value
,其中 item
是列表中当前正在排序的内容,value
是排序依据的值。
以下是 computer-science-in-javascript 的快速排序版本,添加了 "lookup" 函数。
// the swap function doesn't need to change
function swap(items, firstIndex, secondIndex) {
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}
// you will need to pass the lookup function into partition
function partition(items, left, right, lookup) {
// here you need to use "lookup" to get the proper pivot value
var pivot = lookup(items[Math.floor((right + left) / 2)]),
i = left,
j = right;
while (i <= j) {
// here is where you will do a lookup instead of just "items[i]"
while (lookup(items[i]) < pivot) {
i++;
}
// also use lookup here
while (lookup(items[j]) > pivot) {
j--;
}
if (i <= j) {
swap(items, i, j);
i++;
j--;
}
}
return i;
}
function quickSort(items, left, right, lookup) {
var index;
// performance - don't sort an array with zero or one items
if (items.length > 1) {
// fix left and right values - might not be provided
left = typeof left != "number" ? 0 : left;
right = typeof right != "number" ? items.length - 1 : right;
// set a default lookup function just in case
if (typeof lookup !== 'function') {
// the default lookup function just returns the item passed in
lookup = function (item) {
return item;
};
}
index = partition(items, left, right, lookup);
if (left < index - 1) {
quickSort(items, left, index - 1, lookup);
}
if (index < right) {
quickSort(items, index, right, lookup);
}
}
return items;
}
然后您的数据集的 "lookup" 函数将是...
function kmLookup( data ) {
return data.KM;
}
...啊 higher-order 函数的力量
附带说明一下,如果您没有真正接受快速排序,您可以选择懒人选项(或智能选项,具体取决于您的观点)并在 Array 原型上使用 sort
方法...
var mymain = JSON.parse(localStorage.getItem("register"));
// the sort function sorts the array in place,
// so after this next line mymain will be sorted
mymain.sort(function (a, b) {
return a.KM - b.KM;
});
假设您没有使用非常非常大的数据集,那可能是最好的解决方案。 MDN Array.prototype.sort() docs
您好,我正在尝试通过其 ID 对 localStorage 中的一些数据进行排序。
数据的格式是这样的: {"Brand":"Acura","Model":"Choose your model","Age":"1998","KM":"10","Name" :"Harry","ContactInfo":"123456677"}
我想按 KM 值从低到高的顺序排序。
我的本地存储密钥已注册,这是我正在使用的代码:
function Qsort() {
var mymain = JSON.parse(localStorage.getItem("register"));
var result = quicksort(mymain, 0, mymain.length - 1);
// the following line fo code shows the final result of the sorted numbers or strings
console.log(mymain);
console.log(quicksort(mymain, 0, mymain.length - 1));
return result;
}
function quicksort(mymain, left, right) {
var index;
// checks if there are more than one numbers
if (mymain.length > 1) {
// partition will find a pivot then split leist in two either left of right.
// Left list contains everything that is smaller than the pivot and the right contians everythign larger than pivot
index = partition(mymain, left, right);
// will treat left side aas a new problem and will then run the sort
if (left < index - 1){
quicksort(mymain, left, index - 1)
}
// will treat right side as a new problem and will then run the sort
if (index < right) {
quicksort(mymain, index, right)
}
}
return mymain
}
// Divides the whole function
function partition(mymain, left, right) {
var pivot = mymain[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (mymain[i] < pivot) {
i++;
}
while (mymain[j] > pivot) {
j--;
}
if (i <= j) {
swap(mymain, i, j);
i++;
j--;
}
}
return i;
}
// swaps the values based on how high or low the number is
function swap(mymain, firstIndex, secondIndex) {
var temp = mymain[firstIndex];
mymain[firstIndex] = mymain[secondIndex];
mymain[secondIndex] = temp;
}
我应该如何处理 var mymain
部分,以便它只获取 KM 下定义的值。
假设 localStorage
中 "register"
处的项目是一个数组,您可以简单地做...
var mymain = JSON.parse(localStorage.getItem("register"))
var justKMs = mymain.map(function(data){ return data.KM; });
但这会 return 一个仅包含 KM 值的数组,因此您最终将不得不搜索 mymain
列表以获取其余数据,我假设是不是你想要的。
另一种方法是添加一个 "lookup" 函数来告诉快速排序 "how" 获取要用于排序的值。让我们将查找函数签名简单地定义为 function lookup( item ) => value
,其中 item
是列表中当前正在排序的内容,value
是排序依据的值。
以下是 computer-science-in-javascript 的快速排序版本,添加了 "lookup" 函数。
// the swap function doesn't need to change
function swap(items, firstIndex, secondIndex) {
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}
// you will need to pass the lookup function into partition
function partition(items, left, right, lookup) {
// here you need to use "lookup" to get the proper pivot value
var pivot = lookup(items[Math.floor((right + left) / 2)]),
i = left,
j = right;
while (i <= j) {
// here is where you will do a lookup instead of just "items[i]"
while (lookup(items[i]) < pivot) {
i++;
}
// also use lookup here
while (lookup(items[j]) > pivot) {
j--;
}
if (i <= j) {
swap(items, i, j);
i++;
j--;
}
}
return i;
}
function quickSort(items, left, right, lookup) {
var index;
// performance - don't sort an array with zero or one items
if (items.length > 1) {
// fix left and right values - might not be provided
left = typeof left != "number" ? 0 : left;
right = typeof right != "number" ? items.length - 1 : right;
// set a default lookup function just in case
if (typeof lookup !== 'function') {
// the default lookup function just returns the item passed in
lookup = function (item) {
return item;
};
}
index = partition(items, left, right, lookup);
if (left < index - 1) {
quickSort(items, left, index - 1, lookup);
}
if (index < right) {
quickSort(items, index, right, lookup);
}
}
return items;
}
然后您的数据集的 "lookup" 函数将是...
function kmLookup( data ) {
return data.KM;
}
...啊 higher-order 函数的力量
附带说明一下,如果您没有真正接受快速排序,您可以选择懒人选项(或智能选项,具体取决于您的观点)并在 Array 原型上使用 sort
方法...
var mymain = JSON.parse(localStorage.getItem("register"));
// the sort function sorts the array in place,
// so after this next line mymain will be sorted
mymain.sort(function (a, b) {
return a.KM - b.KM;
});
假设您没有使用非常非常大的数据集,那可能是最好的解决方案。 MDN Array.prototype.sort() docs