Javascript 按多个标准对汽车进行分类

Javascript 按多个标准对汽车进行分类,javascript,Javascript,我有一个功能,应该能够比较汽车的基础上,年,类型,品牌等,但我有麻烦让它工作 我对汽车的定义如下: function Automobile( year, make, model, type ){ this.year = year; //integer (ex. 2001, 1995) this.make = make; //string (ex. Honda, Ford) this.model = model; //string (ex. Accord, Focus)

我有一个功能,应该能够比较汽车的基础上,年,类型,品牌等,但我有麻烦让它工作

我对汽车的定义如下:

function Automobile( year, make, model, type ){
    this.year = year; //integer (ex. 2001, 1995)
    this.make = make; //string (ex. Honda, Ford)
    this.model = model; //string (ex. Accord, Focus)
    this.type = type; //string (ex. Pickup, SUV)
}

var automobiles = [ 
    new Automobile(1995, "Honda", "Accord", "Sedan"),
    new Automobile(1990, "Ford", "F-150", "Pickup"),
    new Automobile(2000, "GMC", "Tahoe", "SUV"),
    new Automobile(2010, "Toyota", "Tacoma", "Pickup"),
    new Automobile(2005, "Lotus", "Elise", "Roadster"),
    new Automobile(2008, "Subaru", "Outback", "Wagon")
    ];
上面的部分没有问题,但是我的排序函数有一些问题,我不能理解它们是什么。运行此代码会出现以下错误:

未捕获的语法错误:输入意外结束

/*此函数使用任意比较器对数组进行排序。您向它传递一个比较器和一个适合该比较器的对象数组,它将返回一个新数组,该数组按照索引0中最大的对象和最后一个索引中最小的对象进行排序*/
函数排序器(比较器、数组){
var=数组;
变量最小值;变量温度;
对于(var i=0;iint2){
返回true;
}否则{
返回false;
}
}
/*对于所有比较器,如果车辆根据比较规则“并列”,则不指定这些“并列”车辆的顺序,并且任何一个都可以先到*/
/*这将根据年份对两辆汽车进行比较。新车比旧车“更大”*/
功能比较器(自动1、自动2){
返回exComparator(auto1.make,auto2.make);
}
/*这两种汽车的品牌进行了比较。它应该不区分大小写,并且在字母表中按字母顺序较早的make比后面出现的make“更大”*/
函数makeComparator(自动1、自动2){
返回exComparator(auto1.make,auto2.make);
}
/*这将根据两种汽车的类型进行比较。从“最大”到“最小”的顺序如下:敞篷车、皮卡车、suv、货车(未列出其他类型)。它应该不区分大小写。如果两辆车的类型相同,则按车型年划分的最新车型应被视为“更大”*/
功能类型比较器(自动1、自动2){
var auto1_type=开关(auto1.type.toLowerCase()){
案例(“跑车”):返回5;
案例(“皮卡”):返回4;
案例(“suv”):返回3;
箱子(“货车”):返回2;
案例(“轿车”):返回1;
}
var auto2_type=开关(auto2.type.toLowerCase()){
案例(“跑车”):返回5;
案例(“皮卡”):返回4;
案例(“suv”):返回3;
箱子(“货车”):返回2;
案例(“轿车”):返回1;
}
如果(自动1\u类型>自动2\u类型){
返回auto1.type;
}
否则如果(自动2\u类型>自动1\u类型){
返回auto2.type;
}
否则{
如果(自动1.year>自动2.year){
返回auto1.type;
}
否则{
返回auto2.type;
}
}
}
函数printArr(数组){
对于(var i=0;i
您在
if
语句中忘记了一个大括号
{

if (comp) <-- here
    min = j;
}
var auto1_type =
    switch (auto1.type.toLowerCase()) {
        case "roadster":
            return 5;
...
您的
开关
语句应该如下所示

var auto1_type;
switch (auto1.type.toLowerCase()) {
    case "roadster":
        auto1_type = 5;
    case ....

return auto1_type; 

仅举一些示例,说明如何在不使用
开关的情况下对回调进行排序,而是使用哈希表

功能汽车(年份、品牌、型号、类型){
this.year=year;//整数(例如2001年、1995年)
this.make=make;//字符串(例如本田、福特)
this.model=model;//字符串(例如Accord,Focus)
this.type=type;//字符串(例如皮卡、SUV)
}
函数比较器(a,b){
返回a.year-b.year;
}
函数生成比较器(a,b){
返回a.make.localeCompare(b.make);
}
函数模型比较器(a,b){
返回a.model.localeCompare(b.model);
}
函数类型比较器(a,b){
var类型={跑车:5辆,皮卡车:4辆,suv:3辆,货车:2辆,轿车:1辆},
aa=类型[a.type.toLowerCase()]| | 0,
bb=types[b.type.toLowerCase()]| | 0;
返回aa-bb;
}
var汽车=[新车(1995年,“本田”、“雅阁”、“轿车”)、新车(1990年,“福特”、“F-150”、“皮卡车”)、新车(2000年,“GMC”、“塔霍”、“SUV”)、新车(2010年“丰田”、“塔科马”、“皮卡车”)、新车(2005年“莲花”、“伊利斯”、“跑车”)、新车(2008年“斯巴鲁”、“内地”、“货车”);
汽车.分类(比较法);
document.write('按年份排序:'+JSON.stringify(automobiles,0,4)+'');
汽车.分类(makeComparator);
document.write('按make:'+JSON.stringify(automobiles,0,4)+''排序);
汽车.分类(型号比较器);
document.write('sortedbymodel:'+JSON.stringify(automobiles,0,4)+'');
汽车.分类(类型比较仪);
document.write('按类型排序:'+JSON.stringify(0,4)+'');
//奖金链式排序回调,按类型DESC和年份DESC排序
汽车.分类(功能(a,b){
返回-类型比较器(a,b)| |-年份比较器(a,b);
});
document.write('按类型DESC和年份DESC排序:'+JSON.stringify(automobiles,0,4)+'');
var auto1_type;
switch (auto1.type.toLowerCase()) {
    case "roadster":
        auto1_type = 5;
    case ....

return auto1_type;