如何排序名称的数组,有关在相同的对象名称的整数的值? [英] How to sort an array of names, relevant to the value of an integer in the same object as the name?

查看:97
本文介绍了如何排序名称的数组,有关在相同的对象名称的整数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是下面阵列的对象

 函数员工(姓名,preference,男人,最大值){
  //定义与相关领域的对象雇员
  this.name =名称;
  为了//插槽preference
  这preference = preference。
  //强制要求的插槽数
  this.man =人;
  //最大插槽数可分配
  this.max = MAX;
}

这是下面的数组。第二个字段中的值(它重新在时间表present插槽)由preference已订购。我希望能够选择一个特定的插槽,并提醒它包含了所有那些谁在他们的preference领域和谁放在最高preference它的顺序列表。

  VAR人员=新的Array();
staff.push(新员工(约翰福音,[1,2,3],1,3));
staff.push(新员工(康拉德,[2,1,4],1,3));
staff.push(新员工(长尾,[8,2,6,7,1],3,5));
staff.push(新员工(莎拉,[3,1,4,2,6],3,5));
staff.push(新员工(艾米丽,[7,2,8,1,4],3,5));
staff.push(新员工(标记,[3,4,1,2],1,3));
staff.push(新员工(露西,[5,1,4],1,3));
staff.push(新员工(山姆,[6,2,7],1,3));
showEmployees(工作人员);


解决方案

有3个步骤是:


  1. 过滤列表,只让人们与preference - 使用的 过滤器()

  2. 排序结果由preference位置命令 - 使用的 排序()

  3. 转换结果以逗号分隔的名称字符串中的警报显示 - 使用的 地图()

\r
\r

函数员工(姓名,preference,男人,最大值){\r
    //定义与相关领域的对象雇员\r
    this.name =名称;\r
    为了//插槽preference\r
    这preference = preference。\r
    //强制要求的插槽数\r
    this.man =人;\r
    //最大插槽数可分配\r
    this.max = MAX;\r
\r
}\r
\r
VAR人员=新的Array();\r
staff.push(新员工(john的,[1,2,3],1,3));\r
staff.push(新员工(康拉德,[2,1,4,1,3));\r
staff.push(新员工(埃利奥特,[8,2,6,7,1],3,5));\r
staff.push(新员工(萨拉,[3,1,4,2,6],3,5));\r
staff.push(新员工(艾米丽,[7,2,8,1,4],3,5));\r
staff.push(新员工(标记,[3,4,1,2,1,3));\r
staff.push(新员工(露西,[5,1,4],1,3));\r
staff.push(新员工(山姆,[6,2,7],1,3));\r
\r
//将preference来搜索\r
VAR preF = 2;\r
\r
VAR的结果= staff.filter(函数(五){\r
    如果preF是列表//返回true\r
    返回v preference.indexOf(preF)GT。 -1;\r
})排序(功能(A,B){\r
    //比较每个preference列表pre的位置\r
    返回preference.indexOf(preF)LT。湾preference.indexOf(preF)? -1\r
        :A preference.indexOf(preF)>湾preference.indexOf(preF)? 1:0;\r
})。图(函数(五){\r
    //只是返回的人的名字\r
    返回e.name;\r
})加入(,); //加入名转换成逗号分隔的列表\r
\r
警报(结果);

\r

\r
\r

This is the object for the array below.

function Employee (name,preference,man,max){
  // Defines the object employee with the relevant fields
  this.name = name;
  // slot preference in order
  this.preference = preference;
  // Number of mandatory slots required
  this.man = man;
  // Maximum number of slots that can be allocated
  this.max = max;
}       

This is the array below. The second fields values (which represent slots in a timetable) are ordered by preference already. I want to be able to choose a particular slot and alert a list which contains all those who have it in their preference field and in order of who placed it of the highest preference.

var staff = new Array();
staff.push(new Employee("john",[1,2,3],1,3));
staff.push(new Employee("Conrad",[2,1,4],1,3));
staff.push(new Employee("Elliot",[8,2,6,7,1],3,5));
staff.push(new Employee("Sarah",[3,1,4,2,6],3,5));
staff.push(new Employee("Emily",[7,2,8,1,4],3,5));
staff.push(new Employee("Mark",[3,4,1,2],1,3));
staff.push(new Employee("Lucy",[5,1,4],1,3));
staff.push(new Employee("Sam",[6,2,7],1,3));
showEmployees(staff);

解决方案

There are 3 steps to this:

  1. Filter the list to only get people with that preference - use filter().
  2. Sort the result to order by preference position - use sort().
  3. Convert the results to a comma-separated string of names to show in the alert - use map().

function Employee(name, preference, man, max) {
    // Defines the object employee with the relevant fields
    this.name = name;
    // slot preference in order
    this.preference = preference;
    // Number of mandatory slots required
    this.man = man;
    // Maximum number of slots that can be allocated
    this.max = max;

}

var staff = new Array();
staff.push(new Employee("john", [1, 2, 3], 1, 3));
staff.push(new Employee("Conrad", [2, 1, 4], 1, 3));
staff.push(new Employee("Elliot", [8, 2, 6, 7, 1], 3, 5));
staff.push(new Employee("Sarah", [3, 1, 4, 2, 6], 3, 5));
staff.push(new Employee("Emily", [7, 2, 8, 1, 4], 3, 5));
staff.push(new Employee("Mark", [3, 4, 1, 2], 1, 3));
staff.push(new Employee("Lucy", [5, 1, 4], 1, 3));
staff.push(new Employee("Sam", [6, 2, 7], 1, 3));

// the preference to search on
var pref = 2;

var results = staff.filter(function (v) {
    // return true if pref is in the list
    return v.preference.indexOf(pref) > -1;
}).sort(function (a, b) {
    // compare position of pre in each preference list
    return a.preference.indexOf(pref) < b.preference.indexOf(pref) ? -1
        : a.preference.indexOf(pref) > b.preference.indexOf(pref) ? 1 : 0;
}).map(function (e) {
    // just return the name of the person
    return e.name;
}).join(', '); // join names into comma-separated list

alert(results);

这篇关于如何排序名称的数组,有关在相同的对象名称的整数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆