Javascript sort alphabetically matching the beginning of string then alphabetically for contained text
Date : March 29 2020, 07:55 AM
it should still fix some issue You can split the data into two arrays, one that starts with your input and one that doesn't. Sort each separately, then combine the two results: var data = [
'pizzeria',
'berpizzo',
'apizzetto',
'pizza'
];
function sortInputFirst(input, data) {
var first = [];
var others = [];
for (var i = 0; i < data.length; i++) {
if (data[i].indexOf(input) == 0) {
first.push(data[i]);
} else {
others.push(data[i]);
}
}
first.sort();
others.sort();
return(first.concat(others));
}
var results = sortInputFirst('piz', data);
|
jQuery/JS sort user list alphabetically in groups?
Date : March 29 2020, 07:55 AM
Hope this helps I managed to take bits out of each answer, combine it with other answers here on StackOverflow, and this is what I came up with: // instead of creating on the fly like lintu, I make the arrays beforehand
var tempSort = {online:[],away:[],offline:[]};
$('li').each(function() {
// insert array [uid, name] into corresponding status array
tempSort[$(this).attr('data-status')].push([$(this).attr('data-uid'), $(this).text()]);
});
// sort each status block by name (second multidimensional array item)
for(var status in tempSort) {
tempSort[status].sort(function(a, b) { if (a[1]<b[1]) return -1; if (a[1]>b[1]) return 1; return 0; });
}
|
Sort an array of user input names alphabetically ascending and descending in c
Tag : c , By : Guy Kastenbaum
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Firstm there are some bugs: 1、you can't store 10 person names in one single-dimension array, you need 2D array, just char names[10][10]. #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp1(const void* l, const void* r)
{
return strcmp((char*)l , (char*) r);
}
int cmp2(const void* l, const void* r)
{
return -strcmp((char*)l , (char*) r);
}
int main()
{
char names[10][10] = {0};
char temp[10];
int count;
int sort;
count=1;
for (int i=0;i<10;i++)
{
printf("Please enter name %i\n",i+1);
scanf("%s",names[i]);
}
printf("Would you like to print in Ascending (press 1) or Descending (press 2) order?\n\n");
scanf("%d",&sort);
switch (sort)
{
case 1:
printf("\n Ascending order:\n ");
qsort(names, 10, 10, cmp1);
break;
case 2:
printf("\n Descending order:\n ");
qsort(names, 10, 10, cmp2);
break;
}
for (int i = 0; i < 10; i ++)
{
printf("%s\n", names[i]);
}
return 0;
}
|
How to alphabetically sort of a string delimited by commas and sort alphabetically by members last name?
Tag : python , By : nickthecook
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a string like so: , Your logic can be simplified, as sorted has a key argument: res = sorted(students.split(','), key=lambda x: x.split()[1])
['Jane Bee janebee@gmail.com 555-555-5555',
'John Dee johndee@gmail.com 555-555-5555',
'Sarah Zee sarahzee@gmail.com 555-555-5555']
|
Sort user input alphabetically with AngularJS
Date : March 29 2020, 07:55 AM
it should still fix some issue I would suggest you use arrays instead as they are easier to handle. This way all you need to do is sort the array and then join the elements in the array with a ,. I'm using filter to filter out all the null or undefined values before sorting so that they are omitted. Use localeCompare and convert your strings to lowercase during sort so that it will work correctly even if there are capital letters in your input. Also, since Italian words contain accents/diacritics (Eg. è or ò), you will need to normalize the input before you sort the array. angular.module('app', [])
.controller('ctrl', ['$scope', ($scope) => {
$scope.keywordsIT = new Array(5);
$scope.keywordsEN = new Array(5);
$scope.sortKeywordsIT = function() {
return $scope.keywordsIT
.filter(keyword => !!keyword)
.map(keyword => keyword.normalize("NFD"))
.sort((k1, k2) => k1.toLowerCase().localeCompare(k2.toLowerCase()))
.join(', ');
}
$scope.sortKeywordsEN = function() {
return $scope.keywordsEN
.filter(keyword => !!keyword)
.sort((k1, k2) => k1.toLowerCase().localeCompare(k2.toLowerCase()))
.join(', ');
}
}]);
<script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />
<body ng-app="app" ng-controller="ctrl">
<form>
<fieldset class="border rounded">
<legend class="alert alert-info">Keywords</legend>
<div class="form-group row" id="IT-keywords">
<label for="keywordsIT" class="col-auto col-form-label">Five <span class="badge badge-pill badge-warning">Italian</span> keywords:</label>
<div class="col">
<input type="text" class="form-control" name="keywordsIT" id="keywordsIT1" ng-model="keywordsIT[0]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsIT" id="keywordsIT2" ng-model="keywordsIT[1]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsIT" id="keywordsIT3" ng-model="keywordsIT[2]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsIT" id="keywordsIT4" ng-model="keywordsIT[3]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsIT" id="keywordsIT5" ng-model="keywordsIT[4]">
</div>
</div>
<div class="form-group row" id="EN-keywords">
<label for="keywordsEN" class="col-auto col-form-label">Five <span class="badge badge-pill badge-warning">English</span> keywords:</label>
<div class="col">
<input type="text" class="form-control" name="keywordsEN" id="keywordsEN1" ng-model="keywordsEN[0]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsEN" id="keywordsEN2" ng-model="keywordsEN[1]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsEN" id="keywordsEN3" ng-model="keywordsEN[2]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsEN" id="keywordsEN4" ng-model="keywordsEN[3]">
</div>
<div class="col">
<input type="text" class="form-control" name="keywordsEN" id="keywordsEN5" ng-model="keywordsEN[4]">
</div>
</div>
</fieldset>
</form>
<p>My five Italian keywords: {{sortKeywordsIT()}}.</p>
<p>My five English keywords: {{sortKeywordsEN()}}.</p>
</body>
|