Javascript 排序和合并数组

Javascript 排序和合并数组,javascript,html,arrays,sorting,Javascript,Html,Arrays,Sorting,我必须合并两个数组,并根据比较结果按创建的值排序。我不想使用任何内置的js函数,比如sort。我尝试使用while循环,但无法找到确切的解决方案。以下是我的示例代码: function merge(a, b, n, m) { res = []; i = 0; j = 0; k = 0; while(i < n && j < m) { if(a[i]['created'] < b[j]['created'])

我必须合并两个数组,并根据比较结果按创建的值排序。我不想使用任何内置的js函数,比如sort。我尝试使用while循环,但无法找到确切的解决方案。以下是我的示例代码:

function merge(a, b, n, m)  
{  
    res = []; 
    i = 0; j = 0; k = 0;  
    while(i < n && j < m) {
        if(a[i]['created'] < b[j]['created']) {
            res.push(a[i]);
            i++;
        } else {
            res.push(b[j]);
            j++;
        }        
    }

    while(i < n) {
        res.push(a[i]);
        i++;
    }

    while(j < m) {
        res.push(b[j]);
        j++;
    }
    return res;  
}  

a = [{'title':'title1', 'created':'18'},{'title':'title2', 'created':'16'},{'title':'title3', 'created':'20'}];  
b = [{'title':'title4','created':'17'},{'title':'title5','created':'19'}];  
n = a.length;  
m = b.length;  

var endResult = merge(a, b, n, m);  
console.log(endResult);
请让我知道我错过了什么


注意:我不想使用类似sort()的内置Javascript函数。我必须根据特定的业务逻辑对值进行排序,我将在计算出基本排序后实现这些逻辑。

您可以使用嵌套循环并在concat之后对数组进行排序

函数排序(数组){
var排序=[],
i=数组长度,
J
而(我--){
对于(j=0;j

.as console wrapper{max height:100%!important;top:0;}
您可以使用嵌套循环并在concat之后对数组进行排序

函数排序(数组){
var排序=[],
i=数组长度,
J
而(我--){
对于(j=0;j
.as console wrapper{max height:100%!important;top:0;}
一个简单的O(n^2)解决方案是在所有元素中循环查找最低值,然后再次循环查找第二个最低值,以此类推

函数合并排序(a、b){
var数组=a.concat(b);
var length=array.length;
var结果=[];
while(results.length
一个简单的O(n^2)解决方案是在所有元素中循环寻找最低值,然后再次循环寻找第二个最低值,以此类推

函数合并排序(a、b){
var数组=a.concat(b);
var length=array.length;
var结果=[];
while(results.length

这似乎工作得很好,尽管我不能谈论它的速度

  const arr1 = [111,2,300,50,6,71,9];
  const arr2 = [122,8,40,29,611,74,1];
  // Combines the two arrays into one
  const unsorted = arr1.concat(arr2);
  const sorted = [];

  for(i = 0; i < unsorted.length; i++){
    // Adds all elements from the unsorted array to the destination (sorted) array
    insert(unsorted[i], sorted);
  }
  console.log(sorted);

  function insert(item, arr){
    // Adds the first item automatically
    if(arr.length == 0){
      arr.push(item); 
      return; 
    }
    for(let i = 0; i < arr.length; i++){
      if(i + 1 == arr.length){
        // Adds the item at the end of the array because it's so big 
        arr.push(item);
        break;
      }

      else if(item < arr[i]){
        // Adds the item at the appropriate position in the sorted array
        arr.splice(i, 0, item);
        break;
      }
    }
  }
const arr1=[111,2300,50,6,71,9];
常数arr2=[122,8,40,29611,74,1];
//将两个阵列合并为一个阵列
const unsorted=arr1.concat(arr2);
常量排序=[];
对于(i=0;i
这似乎工作得很好,尽管我无法形容它的速度

  const arr1 = [111,2,300,50,6,71,9];
  const arr2 = [122,8,40,29,611,74,1];
  // Combines the two arrays into one
  const unsorted = arr1.concat(arr2);
  const sorted = [];

  for(i = 0; i < unsorted.length; i++){
    // Adds all elements from the unsorted array to the destination (sorted) array
    insert(unsorted[i], sorted);
  }
  console.log(sorted);

  function insert(item, arr){
    // Adds the first item automatically
    if(arr.length == 0){
      arr.push(item); 
      return; 
    }
    for(let i = 0; i < arr.length; i++){
      if(i + 1 == arr.length){
        // Adds the item at the end of the array because it's so big 
        arr.push(item);
        break;
      }

      else if(item < arr[i]){
        // Adds the item at the appropriate position in the sorted array
        arr.splice(i, 0, item);
        break;
      }
    }
  }
const arr1=[111,2300,50,6,71,9];
常数arr2=[122,8,40,29611,74,1];
//将两个阵列合并为一个阵列
const unsorted=arr1.concat(arr2);
常量排序=[];
对于(i=0;i
使用内置的
sort
concat
功能,这是非常容易做到的

可以向比较器功能提供自定义逻辑。很难想象有什么必要的排序不能这样做

/`comparator`包含您的自定义业务逻辑。如果`a,则返回负数`
//比'b'小`