2016-04-22 18 views
1

Ich habe eine Funktion, die in der Lage sein soll, Autos basierend auf Jahr, Typ, Marke usw. zu vergleichen, aber ich habe Schwierigkeiten, es zur Arbeit zu bringen.Sortieren von Autos nach mehreren Kriterien

definiere ich meine Autos wie folgt aus:

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

Der obige Teil keine Probleme hat, aber meine Sortierfunktion hat einige Probleme, und ich kann nicht herausfinden, was sie sind. Ausführen dieses Codes gibt mir den Fehler:

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

Antwort

1

nur einige Beispiele aussehen sollte.

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

Sie vergessen, eine geschweifte Klammer { in der if Anweisung

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

Auch Sie haben falsch switch Aussage, versuchen Sie den Wert von return zuweisen Aussage

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

Ihre switch Aussage wie diese für die Sortierung statt Rückrufe ohne switch, aber mit einer Hash-Tabelle

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

return auto1_type; 
Verwandte Themen