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

时间:2016-04-22 16:07:08

标签: javascript

我有一个应该能够根据年份,类型,制造等比较汽车的功能,但是我无法让它工作。

我这样定义我的汽车:

cmd = new OleDbCommand("insert into empl values(@DateJobFinished),cn);
cmd.Parameters.Add("DateJobFinished", OleDbType.DBDate);
cmd.Parameters["DateJobFinished"].Value = DateTime.Now;

上面的部分没有问题,但我的排序功能有一些问题,我无法弄清楚它们是什么。运行此代码会给我错误:

  

未捕获的SyntaxError:意外的输入结束

cmd.Parameters.AddWithValue("@DateJobFinished", datetimefordatabase(DateTime.Now));

 private DateTime datetimefordatabase(DateTime d)
    {
        return new DateTime(d.Year, d.Month, d.Day);
    }

2 个答案:

答案 0 :(得分:1)

仅举几个例子来排序没有switch的回调,而是使用哈希表。



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

function yearComparator(a, b) {
    return a.year - b.year;
}

function makeComparator(a, b) {
    return a.make.localeCompare(b.make);
}

function modelComparator(a, b) {
    return a.model.localeCompare(b.model);
}

function typeComparator(a, b) {
    var types = { roadster: 5, pickup: 4, suv: 3, wagon: 2, sedan: 1 },
        aa = types[a.type.toLowerCase()] || 0,
        bb = types[b.type.toLowerCase()] || 0;
    return aa - bb;
}

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")];

automobiles.sort(yearComparator);
document.write('<pre>sorted by year: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
automobiles.sort(makeComparator);
document.write('<pre>sorted by make: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
automobiles.sort(modelComparator);
document.write('<pre>sorted by model: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
automobiles.sort(typeComparator);
document.write('<pre>sorted by type: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');

// bonus chained sort callbacks, get sorted by type DESC and year DESC
automobiles.sort(function (a, b) {
    return -typeComparator(a, b) || -yearComparator(a, b);
});
document.write('<pre>sorted by type DESC and year DESC: ' + JSON.stringify(automobiles, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您忘记了{声明中的大括号if

if (comp) <-- here
    min = j;
}

您还有错误的switch语句,您正尝试从return语句中分配值

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

您的switch声明应如下所示

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

return auto1_type;