2017-01-04 4 views
3

Meine Aufgabe ist es, eine Zeichenfolge, zum Beispiel zu nehmen:Wie kann ich einen String alphabetisch sortieren, wenn der String sowohl Zahlen als auch Buchstaben enthält?

"24z6 1x23 y369 89a 900b"

Und nun die Buchstaben aus dem String entfernen und die Zeichenfolge wie so zurück:

89 900 123 369 246 (geordnet nach dem Buchstaben)

+0

sollen selbst sein: 1) Split Zeichenfolge auf Raum, 2) Schreiben von jeder Unterkette zu extrahieren, um es in die Number Mapping es darstellt, 3) zu sortieren, 4) Buchstaben entfernen, 5) print Ergebnis. – MeterLongCat

Antwort

2

Sie könnten nach Leerzeichen sortieren, nach Buchstaben sortieren, nur Zahlen nummerieren und für das Ergebnis beitreten.

var string = "24z6 1x23 y369 89a 900b", 
 
    result = string.split(' ').sort(function (a, b) { 
 
     return a.match(/\D+/g).join('').localeCompare(b.match(/\D+/g).join('')); 
 
    }).map(function (a) { 
 
     return a.match(/\d/g).join(''); 
 
    }).join(' '); 
 

 
console.log(result); // '89 900 123 369 246'

2

Ein Ansatz ist:

// here we split the string of alphanumerics by a string 
 
// of one or more ('+') white-characters, using 
 
// String.prototype.split() with a regular expression 
 
// to find white-space characters ('\S'), then 
 
// use the anonymous function of the Array.prototype.sort() 
 
// function: 
 
var sorted = "24z6 1x23 y369 89a 900b".split(/\s+/).sort(function(a, b) { 
 
    // a: the first element being compared, 
 
    // b: the second element being compared 
 

 
    // in each we find the first alphabetical character 
 
    // using String.prototype.match() with a regular expression 
 
    // to match alphabetical characters (\D); and sorting 
 
    // based on whether the assessment of a... > b... is true 
 
    // or false. 
 
    return a.match(/\D/) > b.match(/\D/); 
 

 
// iterating over the sorted array using Array.prototype.map(): 
 
}).map(function(el) { 
 
    // el is the current array-element of the Array over 
 
    // we're iterating. 
 

 
    // here we return the current Array element having first 
 
    // used String.prototype.replace() to remove all 
 
    // alphabetical characters (\D) globally (g) from the String: 
 
    return el.replace(/\D/g, ''); 
 
}); 
 

 
console.log(sorted); // ["89", "900", "123", "369", "246"]

+0

Ergebnis sieht anders aus als erwartet ... –

+0

Ah; Ich habe vergessen, das Array wieder zusammen zu schließen. Naja; Ich hatte mich über die Down-Abstimmung gewundert ... –

+0

es ist nicht der Beitritt, sondern die Reihenfolge. –

0

Ein offensichtlicher Ansatz scheint dies zu sein:

var str = "24z6 1x23 y369 89a 900b"; 
 
var strArray = str.split(' '); 
 
console.log(strArray); // prints ["24z6", "1x23", "y369", "89a", "900b"] 
 

 
// Then we sort the array alphabetically using Regex 
 
strArray.sort(function (a, b) { 
 
    return a.match(/\D/) > b.match(/\D/) 
 
}); 
 
console.log(strArray); // prints ["89a", "900b", "1x23", "y369", "24z6"] 
 

 
// Next we remove the alphabets out of sorted array 
 
var result = strArray.map(function (each) { 
 
    return each.replace(/\D/g, ''); 
 
}); 
 
console.log(result); // prints ["89", "900", "123", "369", "246"] 
 

 
// finally join the array with spaces to get your desired output 
 
var output = result.join(' '); 
 
console.log(output); // prints 89 900 123 369 246

Verwandte Themen