2016-04-22 13 views
1

年、型、製造元などをもとに自動車を比較できるようになっている機能がありますが、問題なく動作するようになっています。自動車を複数の条件で並べ替える

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

上記の部分は何も問題はありませんが、私のソート機能は、いくつかの問題を抱えている、と私は彼らが何であるかを把握することはできません。

私はこのように私の車を定義します。このコードを実行すると、私にエラーを与える:

Uncaught SyntaxError: Unexpected end of input

/*This function sorts arrays using an arbitrary comparator. You pass it a comparator and an array of objects appropriate for that comparator and it will return a new array which is sorted with the largest object in index 0 and the smallest in the last index*/ 
function sortArr(comparator, array){ 
    var sorted = array; 
    var min; var temp; 
    for(var i = 0; i < array.length-1; i++){ 
     min = i; 
     for(var j = i+1; j < array.length; j++){ 
      var comp = comparator(array[j], array[min]); 
      if(comp) 
       min = j; 
      } 
      if(min != i){ 
       temp = sorted[i]; 
       sorted[i] = sorted[min]; 
       sorted[min] = temp; 
      } 
     } 
    return sorted; 
} 

function exComparator(int1, int2){ 
    if (int1 > int2){ 
     return true; 
    } else { 
     return false; 
    } 
} 

/*For all comparators if cars are 'tied' according to the comparison rules then the order of those 'tied' cars is not specified and either can come first*/ 

/*This compares two automobiles based on their year. Newer cars are "greater" than older cars.*/ 
function yearComparator(auto1, auto2){ 
    return exComparator(auto1.make, auto2.make); 
} 

/*This compares two automobiles based on their make. It should be case insensitive and makes which are alphabetically earlier in the alphabet are "greater" than ones that come later.*/ 
function makeComparator(auto1, auto2){ 
    return exComparator(auto1.make, auto2.make); 
} 

/*This compares two automobiles based on their type. The ordering from "greatest" to "least" is as follows: roadster, pickup, suv, wagon, (types not otherwise listed). It should be case insensitive. If two cars are of equal type then the newest one by model year should be considered "greater".*/ 
function typeComparator(auto1, auto2){ 
    var auto1_type = switch(auto1.type.toLowerCase()){ 
     case("roadster"): return 5; 
     case("pickup"): return 4; 
     case("suv"): return 3; 
     case("wagon"): return 2; 
     case("sedan"): return 1; 
    } 

    var auto2_type = switch(auto2.type.toLowerCase()){ 
     case("roadster"): return 5; 
     case("pickup"): return 4; 
     case("suv"): return 3; 
     case("wagon"): return 2; 
     case("sedan"): return 1; 
    } 

    if(auto1_type > auto2_type) { 
     return auto1.type; 
    } 
    else if(auto2_type > auto1_type) { 
     return auto2.type; 
    } 
    else { 
     if(auto1.year > auto2.year) { 
      return auto1.type; 
     } 
     else { 
      return auto2.type; 
     } 
    } 
} 

function printArr(array){ 
    for(var i = 0; i < array.length; i++){ 
     var car = array[i]; 
     console.log(car.year + ' ' + car.make + ' ' + car.model + ' ' + car.type); 
    } 
} 

答えて

1

のようにちょうどいくつかの例を見て、その代わりに、ハッシュテーブルを持つ必要があります。

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

0

あなたはif声明

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

に中括弧{を忘れても、あなたはreturnから値を代入しようとしている、間違ったswitch文を持っていますステートメント

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

あなたswitch文はswitchせずにコールバックをソートするために、この

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

return auto1_type; 
関連する問題