用正则表达式排序对象数组

时间:2016-11-03 09:39:14

标签: javascript arrays regex sorting

我有类似于这个found this post的问题,但我有这样的对象数组:

[  
   {  
      "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."
   }
]

我需要根据用户提取的正则表达式对其进行排序,它应该显示以用户提供的文本开头的第一个应用程序,然后它应该显示应用程序正则表达式匹配描述和工具链,这是我的代码:

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

我正在对角度ng-table数据进行排序,但这不重要。我的代码没有按预期工作,它没有正确排序,我的代码有什么问题?

0 个答案:

没有答案