JavaScript:根据“排序数组”;“外部”;参数

JavaScript:根据“排序数组”;“外部”;参数,javascript,arrays,sorting,Javascript,Arrays,Sorting,在JavaScript中,我有一个对象数组和一个如下所示的输入值: const inputVal = 'foo'; const objArray = [ {title: 'blahaa', type: 'foobar'}, {title: 'foobar', type: 'wohoo'}, {title: 'foobar', type: 'foobar'}, {title: 'foobar', type: 'aaaabaa'}, {title: 'foo

在JavaScript中,我有一个对象数组和一个如下所示的输入值:

const inputVal = 'foo';

const objArray = [
    {title: 'blahaa', type: 'foobar'},
    {title: 'foobar', type: 'wohoo'},
    {title: 'foobar', type: 'foobar'},
    {title: 'foobar', type: 'aaaabaa'},
    {title: 'foobar', type: 'moo'},
    {title: 'aaah', type: 'foogoo'},
    {title: 'foohiii', type: 'foobar'},
    {title: 'aaah', type: 'foobar'},
    {title: 'foodoo', type: 'aaaabaa'},
    {title: 'gaaaaaah', type: 'foobar'},
    {title: 'foobar', type: 'foogoo'},
];
如您所见,数组中的所有元素的属性在标题或类型中都以“foo”开头。此外,所有元素都是唯一的,相同的标题和/或类型可以出现在多个元素中,但两者的相同组合不能出现两次

我想按以下方式对该数组进行排序:

  • 标题和类型都以
    inputVal
  • 按字母顺序打字
  • 标题按字母顺序排列
  • 标题以
    inputVal
    开头,但不是类型
  • 按字母顺序打字
  • 标题按字母顺序排列
  • 类型以
    inputVal
    开头,但不以title开头
  • 按字母顺序打字
  • 标题按字母顺序排列
  • 示例列表的排序如下所示:

    const objArray = [
        {title: 'foobar', type: 'foobar'}, // Criterium 1
        {title: 'foohiii', type: 'foobar'}, // Criterium 1
        {title: 'foobar', type: 'foogoo'}, // Criterium 1
        {title: 'foobar', type: 'aaaabaa'}, // Criterium 2
        {title: 'foodoo', type: 'aaaabaa'}, // Criterium 2
        {title: 'foobar', type: 'moo'}, // Criterium 2
        {title: 'foobar', type: 'wohoo'}, // Criterium 2
        {title: 'aaah', type: 'foobar'}, // Criterium 3
        {title: 'blahaa', type: 'foobar'}, // Criterium 3
        {title: 'gaaaaaah', type: 'foobar'}, // Criterium 3
        {title: 'aaah', type: 'foogoo'}, // Criterium 3
    ];
    

    我尝试将
    array.prototype.sort(callback)
    与几个不同的回调函数一起使用,但似乎没有得到正确的回调函数。有人能帮我吗?

    你可以试试以下方法:

  • 基于上述3个标准,使用array.prototype.filter将阵列拆分为3个子阵列
  • 独立排序3个子阵列,然后使用Array.prototype.concat连接
  • 要提高性能,请异步处理所有子阵列。
    分而治之的胜利

    您可以尝试以下操作:

  • 基于上述3个标准,使用array.prototype.filter将阵列拆分为3个子阵列
  • 独立排序3个子阵列,然后使用Array.prototype.concat连接
  • 要提高性能,请异步处理所有子阵列。
    分而治之的胜利

    您可以输入条件,先按三组排序,然后按值排序

    const
    值='foo',
    开始=(v=>s=>s.startsWith(v))(值),
    数组=[{title:'blahaa',type:'foobar'},{title:'foobar',type:'wohoo'},{title:'foobar',type:'aaaabaa'},{title:'foobar',type:'moo'},{title:'aah',type:'foogoo},{title:'foohii',type:'foobar'},{title:'aah',type:'foobar'},{title:'foodoo,{title:'gaaaaah',type:'foobar'},{title:'foobar',type:'foogoo'}];
    array.sort((a,b)=>
    (开始(b.标题)和开始(b.类型))-(开始(a.标题)和开始(a.标题))
    ||开始(b.标题)-开始(a.标题)
    ||启动(b型)-启动(a型)
    ||a.type.localeCompare(b.type)
    ||a.title.localeCompare(b.title)
    );
    console.log(数组);

    .as console wrapper{max height:100%!important;top:0;}
    您可以输入条件并首先按三个组排序,然后按值排序

    const
    值='foo',
    开始=(v=>s=>s.startsWith(v))(值),
    数组=[{title:'blahaa',type:'foobar'},{title:'foobar',type:'wohoo'},{title:'foobar',type:'aaaabaa'},{title:'foobar',type:'moo'},{title:'aah',type:'foogoo},{title:'foohii',type:'foobar'},{title:'aah',type:'foobar'},{title:'foodoo,{title:'gaaaaah',type:'foobar'},{title:'foobar',type:'foogoo'}];
    array.sort((a,b)=>
    (开始(b.标题)和开始(b.类型))-(开始(a.标题)和开始(a.标题))
    ||开始(b.标题)-开始(a.标题)
    ||启动(b型)-启动(a型)
    ||a.type.localeCompare(b.type)
    ||a.title.localeCompare(b.title)
    );
    console.log(数组);

    .as console wrapper{max height:100%!important;top:0;}
    这里是另一个解决方案,可能由于使用多个正则表达式而效率较低

    const objArray=[
    {标题:'blahaa',键入:'foobar'},
    {标题:'foobar',键入:'wohoo'},
    {标题:'foobar',键入:'foobar'},
    {标题:'foobar',键入:'aaaabaa'},
    {标题:'foobar',键入:'moo'},
    {标题:'aah',键入:'foogoo'},
    {标题:'foohii',键入:'foobar'},
    {标题:'aah',键入:'foobar'},
    {标题:'foodoo',键入:'aaaabaa'},
    {标题:'gaaaaah',键入:'foobar'},
    {标题:'foobar',键入:'foogoo'}
    ];
    const input=“foo”;
    常量Jarray=[
    objArray.sort((a,b)=>{
    常数
    regex=newregexp(“^(“+input+”)(.*”,“i”),
    titleAExec=regex.exec(a.title),
    typeAExec=regex.exec(a.type),
    titleBExec=regex.exec(b.title),
    typeBExec=regex.exec(b.type)
    常数
    titleComparison=TitleExec[2]。localeCompare(TitleExec[2]),
    typeComparison=typeAExec[2]。localeCompare(typeBExec[2])
    if(titleExec[1]&&!titleExec[1])
    返回-1
    else if(titleBExec[1]&&!titleAExec[1])
    返回1
    if(typeAExec[1]&&!typeBExec[1])
    返回-1
    else if(typeBExec[1]&&!typeAExec[1])
    返回1
    如果(类型比较===0)
    返回标题比较
    返回类型比较;
    })
    ]
    
    log(sortedObjArray)
    这里是另一个解决方案,可能由于使用多个正则表达式而效率较低

    const objArray=[
    {标题:'blahaa',键入:'foobar'},
    {标题:'foobar',键入:'wohoo'},
    {标题:'foobar',键入:'foobar'},
    {标题:'foobar',键入:'aaaabaa'},
    {标题:'foobar',键入:'moo'},
    {标题:'aah',键入:'foogoo'},
    {标题:'foohii',键入:'foobar'},
    {标题:'aah',键入:'foobar'},
    {标题:'foodoo',键入:'aaaabaa'},
    {标题:'gaaaaah',键入:'foobar'},
    {标题:'foobar',键入:'foogoo'}
    ];
    const input=“foo”;
    常量Jarray=[
    objArray.sort((a,b)=>{
    常数
    regex=newregexp(“^(“+input+”)(.*”,“i”),
    titleAExec=regex.exec(a.title),
    typeAExec=regex.exec(a.type),
    titleBExec=regex.exec(b.title),
    typeBExec=regex.exec(b.type)
    常数
    山雀