C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# How to sort an array based on the occurrences of the elements in a string in C#?

By : vvarmo
Date : November 22 2020, 10:54 AM
this one helps. You can do this concisely with a Linq query (I've renamed your original array to countries):
code :
``````var result = countries.Select(country => new { country,
.Where(pair => pair.index >= 0)
.OrderBy(pair => pair.index)
.Select(pair => pair.country)
.ToArray();
``````

Share :

## Sort array based on count of occurrences in ascending order

By : Lu Yu
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Here's one approach to start you off could be based on the idea of keeping a count of how many times each integer in the initial array has occurred in a map. Once all the numbers have been counted, sort the map by order of ascending value, and then print the output of the map:
code :
``````import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

public class SortCount {
public static void main(String[] args) {
int nums[] = {0,0,0,1,3,3,2,1,3,5,6,0};
HashMap<Integer,Integer> counts = new HashMap<Integer,Integer>();

for(int i = 0; i < nums.length; i++) {
if(counts.containsKey(nums[
Integer c = counts.get(nums[i]) + 1;
counts.put(nums[i], c);
}
else {
counts.put(nums[i],1);
}
}

ValueComparator<Integer,Integer> bvc = new ValueComparator<Integer,Integer>(counts);
TreeMap<Integer,Integer> sortedMap = new TreeMap<Integer,Integer>(bvc);
sortedMap.putAll(counts);

ArrayList<Integer> output = new ArrayList<Integer>();
for(Integer i : sortedMap.keySet()) {
for(int c = 0; c < sortedMap.get(i); c++) {
}
}

System.out.println(output.toString());
}
}
``````
``````import java.util.Comparator;
import java.util.Map;

public class ValueComparator<T1,T2 extends Comparable<T2>> implements Comparator<T1> {
Map<T1,T2> base;
public ValueComparator(Map<T1,T2> base) {
this.base = base;
}

@Override
public int compare(T1 k1, T1 k2) {
T2 val1 = base.get(k1);
T2 val2 = base.get(k2);

return val1.compareTo(val2);
}
}
``````

## Elasticsearch sort based on the number of occurrences a string appears in an array

By : K. Tanakazima
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further For those curious, I was not able to boost based on how many occurrences of the word happen in the array. I did however accomplished what I needed with the following:
code :
``````curl -X POST "http://localhost:9200/index/document/1" -d '{"id":1,"states_ties":["CA"],"state_abbreviation":"CA","worked_in_states":["CA"],"training_in_states":["CA"]}'
curl -X POST "http://localhost:9200/index/document/2" -d '{"id":2,"states_ties":["CA","NY"],"state_abbreviation":"FL","worked_in_states":["NY","CA"],"training_in_states":["NY","CA"]}'
curl -X POST "http://localhost:9200/index/document/3" -d '{"id":3,"states_ties":["CA","NY","FL"],"state_abbreviation":"NY","worked_in_states":["NY","CA"],"training_in_states":["NY","FL"]}'

curl -X GET 'http://localhost:9200/index/_search?per_page=10&pretty' -d '{
"query": {
"custom_filters_score": {
"query": {
"terms": {
"states_ties": [
"CA"
]
}
},
"filters": [
{
"filter": {
"term": {
"state_abbreviation": "CA"
}
},
"boost": 1.03
},
{
"filter": {
"terms": {
"worked_in_states": [
"CA"
]
}
},
"boost": 1.02
},
{
"filter": {
"terms": {
"training_in_states": [
"CA"
]
}
},
"boost": 1.01
}
],
"score_mode": "multiply"
}
},
"sort": [
{
"_score": "desc"
}
]
}'

results: id: score

1: 0.75584483
2: 0.73383
3: 0.7265643
``````

## Sort Array based on number of character occurrences

By : JH4i
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I would create a class that implements the Comparator interface. In the compare method, detect the number of 5 characters there. If the number of 5 characters is equal, then use Integer.parseInt to get the numbers and compare them.
Make sure to return a number less than zero, 0, or greater than zero if the first string should compare less than, equal to, or greater than the second string respectively, to fulfill compare's contract.

## how to sort a string array based on elements of another List

By : charlie2ez
Date : March 29 2020, 07:55 AM
To fix this issue I've a string array - lets imagine something like; , I think you want to do something like this:
code :
``````using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var values = new List<string>{"a","b","c","d","e","a","b","c","d","e","a","b","c","d","e"};
var order = new List<string>{"a","b","c","d","e"};

var sortedList = SortByList(values, order);

foreach(var i in sortedList)
{
Console.WriteLine(i);
}
}
public static List<string> SortByList(List<string> values, List<string> order){
return values.OrderBy(x => order.IndexOf(x)).ToList();
}
}
``````

## Find all occurrences of elements in an array in a string

By : user2963549
Date : March 29 2020, 07:55 AM
Hope that helps you could use a pipe-delimited regex pattern with the keywords list like this:
code :
``````const subStrings = ["javascript", "php", "python"];
const regex = subStrings.join("|");

db.find ({
body: { \$regex: regex, \$options: 'i' }
});
``````