按姓氏排序数组

时间:2014-06-30 02:49:33

标签: javascript jquery html

作者列表(名字和姓氏)正在发出,它被放置在一个数组中。请求是如何按姓氏排序。

我尝试,但它按名字排序(因为名字在前面)。有没有办法对第二个单词进行排序?请帮帮忙。谢谢!

JSFiddle sample

HTML

 <table id="myTable">
    <tr>
        <td>AUTHOR NAME</td>
    </tr>
 </table>

JS

var authors = ['Ernest Hemingway',
                'Charlotte Bronte',
                'Dante Alighieri',
                'Emily Dickinson'];
authors.sort();
for ( var i=0; i<authors.length; i++)
{
    var tr="<tr>";
    var td="<td>" + authors[i] + "</td></tr>"
    $('#myTable').append(tr+td);

}

4 个答案:

答案 0 :(得分:2)

我认为这对你很重要

authors.sort(function (author1, author2) {
    return author1.split(' ')[1] > author2.split(' ')[1];
});

因为默认情况下它会按名字排序。你必须告诉sort函数你想如何对这个数组进行排序。

答案 1 :(得分:1)

尝试进行自定义排序,例如

authors.sort(function (obj1, obj2) {
    var s1 = obj1.split(' ')[1],
        s2 = obj2.split(' ')[1];
    return (s1 || obj1).localeCompare(s2 || obj2)
});

演示:Fiddle

注意:我假设姓氏将是名称

中第一个空格字符后面的文本

答案 2 :(得分:0)

根据MDN,您可能需要考虑以下事项:

authors.sort(function (a, b) {
    if (a.split(' ')[1] > b.split(' ')[1])
      return 1;
    if (a.split(' ')[1] < b.split(' ')[1])
      return -1;
    return 0;
});

答案 3 :(得分:0)

public partial class MainWindow : Window
{
    
    public ObservableCollection<CityModel> cities { get; set; } = new ObservableCollection<CityModel>();
    public ObservableCollection<PersonModel> people { get; set; } = new ObservableCollection<PersonModel>();
    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        // Create some cities...
        CityModel tokyo = new CityModel() { Id = 0, Name = "Tokyo" };
        CityModel london = new CityModel() { Id = 1, Name = "London" };
        CityModel buenosAires = new CityModel() { Id = 2, Name = "Buenos Aries" };
        cities = new ObservableCollection<CityModel>() { tokyo, london, buenosAires };

        // Create some people...
        PersonModel juan = new PersonModel() { FirstName = "Juan", LastName = "Garcia", City = buenosAires };
        PersonModel bridget = new PersonModel() { FirstName = "Bridget", LastName = "Jones", City = london };
        PersonModel aoife = new PersonModel() { FirstName = "Aoife", LastName = "O'Connor", City = tokyo };
        people = new ObservableCollection<PersonModel>() { juan, bridget, aoife };

        // Workaround in order to expose list of cities along with Person properties...
        foreach (PersonModel person in people) { person.citiesList = cities; }

    }

    private void listBox_SelectionChanged(object sender, RoutedEventArgs e)
    {
        ListBox listBox = sender as ListBox;
        MyPropertyGrid.SelectedObject = listBox.SelectedItem;
    }
}

public class PersonModel
{
    public string FirstName { get; set; } = "";

    public string LastName { get; set; } = "";

    public CityModel City { get; set; } = new CityModel();

    // Workaround in order to expose list of cities along with Person properties...
    public ObservableCollection<CityModel> citiesList { get; set; } = new ObservableCollection<CityModel>();
}

public class CityModel
{
    public int Id { get; set; } = 0;
    public string Name { get; set; } = "";
    public override string ToString() { return Name; }
}