0

I'm getting desired results, but need to order them alphabetically. I am unsure what to put in exactly in the code:

function showClients(data) {
    var html = '';
    for (var clientId in data) {
        html += '<p>' + clientId;
        html += data[clientId].connected ? ' connected' : ' disconnected';
        html += '</p>';
    }
    if (!html) {
        html += '<p>No clients connected</p>';
    }
    $('#connected_clients').html(html);
}
2
  • I think what you're looking for is sorting. Sort the data by alphabet order. Commented Feb 3, 2017 at 6:09
  • What is data, an array or an object? By what do you want to sort, the clientId? Commented Feb 3, 2017 at 6:19

2 Answers 2

1

If I've got the layout of the data structure right, it is a key/value object, which in turn contains objects that have information such as connected.

In that case you can use Object.keys to get the keys first, sort those Object.keys(data).sort(), and create the html from there. For example:

function showClients(data) {
    var html = Object.keys(data).sort().reduce(function(h, clientId){
        return h + '<p>' + clientId
        + (data[clientId].connected ? ' connected' : ' disconnected')
        + '</p>';
    }, '')
        || '<p>No clients connected</p>';

    $('#connected_clients').html(html);
}

function showClients(data) {
    var html = Object.keys(data).sort().reduce(function(h, clientId){
    	return h + '<p>' + clientId
        + (data[clientId].connected ? ' connected' : ' disconnected')
        + '</p>';
    }, '')
    	|| '<p>No clients connected</p>';
    
    $('#connected_clients').html(html);
}


showClients({Client3:{connected:true},Client1:{connected:true},Client2:{connected:false}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=connected_clients></div>

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thank you.
0

Call sort on the data like Array.prototype.sort(data). If you don't pass a compare function, it will sort the elements alphabetically.

function showClients(data) {
    var html = '';
    for (var clientId in Array.prototype.sort.call(data)) {
        html += '<p>' + clientId;
        html += data[clientId].connected ? ' connected' : ' disconnected';
        html += '</p>';
    }
    if (!html) {
        html += '<p>No clients connected</p>';
    }
    $('#connected_clients').html(html);
}

8 Comments

You'd better put Array.prototype.sort(data) before the 'for', i think.
Why ? It will not call the sort every time
No, that is not how to call sort.
But why so complicated with call at all?
data may be not an array, but array like object. So why I called like that
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.