-1
[
  {
    "project_name": "A",
    "key1": "value1"
  },
  {
    "project_name": "B",
    "key1": "value1"
  },
  {
    "project_name": "B",
    "key1": "value1"
  },
  {
    "project_name": "B",
    "key1": "value1"
  },
  {
    "project_name": "A",
    "key1": "value1"
  },
  {
    "project_name": "C",
    "key1": "value1"
  }
]

Consider data longer than this array.

I would like to sort the project_name values ​​starting from the ones with the highest value and not take anything after a certain value in such a data.

For example,

I don't want to take the 2 projects with the highest project_name value and get the rest of the data.

Exepcted output:

[
  {
    "project_name": "B",
    "key1": "value1"
  },
  {
    "project_name": "B",
    "key1": "value1"
  },
  {
    "project_name": "B",
    "key1": "value1"
  },
  {
    "project_name": "A",
    "key1": "value1"
  },
  {
    "project_name": "A",
    "key1": "value1"
  }
]
  • Anything you have tried till now? – Not A Bot Jun 06 '22 at 12:29
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – derpirscher Jun 06 '22 at 12:30
  • I can only rank the ones with the most value – Taha Yıldırım Jun 06 '22 at 12:33
  • The question you mentioned does not solve the problem. – Taha Yıldırım Jun 06 '22 at 12:35
  • How does describing how to use the sort function *not* solve a problem about sorting an array? Alternatively, also the answers to this question https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values describe sorting array quite detailed – derpirscher Jun 06 '22 at 12:36
  • After sorting the arrays I want to get only the highest value holders. Ranking just isn't enough – Taha Yıldırım Jun 06 '22 at 12:39
  • look at expected output not just sorted value – Taha Yıldırım Jun 06 '22 at 12:40
  • the title of the question is only about sorting, so I assumed you know how to use [`Array::slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) or [`Array::splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) to remove the unwanted elements, once the array is sorted – derpirscher Jun 06 '22 at 12:44
  • you can filter with condition then sort result – Xupitan Jun 06 '22 at 12:46
  • @Xupitan How would you filter, if you don't know yet, what the highest value is, to filter for? – derpirscher Jun 06 '22 at 12:46
  • @derpirscher sort + slice is *one* solution. Not necessarily the only solution. Partial duplicates (where the target is a subset of the source) can be misleading. – VLAZ Jun 06 '22 at 12:47
  • @VLAZ I didn't say it was ... – derpirscher Jun 06 '22 at 12:48
  • @TahaYıldırım BTW your expected result is inconsistend with the problem description. You said you want to remove "*the 2 projects with the highest project_name*", yet in your expected result, you have only removed one value from your input data ... – derpirscher Jun 06 '22 at 12:49
  • `not take anything after a certain value in such a data` condition – Xupitan Jun 06 '22 at 12:49
  • @Xupitan Yes, but following the problem description, that "certain value" can only be determined after knowing the two highest values ... So you have to sort first and then filter ... – derpirscher Jun 06 '22 at 12:50
  • @derpirscher "*the title of the question is only about sorting, so I assumed you know how to use Array::slice or Array::splice to remove the unwanted elements, once the array is sorted*" says to me that you expect this to be a duplicate for *sorting* arrays. And then slice/splice to get rid of unwanted items. And that's only a subset of the solutions here. One could also, bucket the results and then extract the "highest" and finally sort. Which is not covered by just sorting the dataset. – VLAZ Jun 06 '22 at 12:52
  • @VLAZ Yes, I expected this question to be solved, once OP knows how to sort, because he specifically asked, about sorting in the title. Yes I assumed, OP would then know how to remove unwanted elements, and provided splice and slice as one possible solution for that problem ... I acknowledge that my assumption seems to be wrong and I will rectract my vote. Nonetheless, I think this is a poor question and should be closed anyways. But instead of just brushing it off with "needs more details" or "needs details or clarity" I thought pointing in the right direction might help anyways – derpirscher Jun 06 '22 at 12:54
  • @derpirscher So, you're suggesting a subset of the solutions. If I were to ask "how do I put up a painting", a duplicate of "how to hammer a nail in a wall" is a partial solution to my problem. It misses other potential solutions that don't involve hammers or nails. – VLAZ Jun 06 '22 at 12:59
  • @VLAZ But OP, didn't just ask "how do I put up a painting". OP asked "**How do I put a nail in the wall** .... so that I can hang a painting". IMHO the title of the question should address the main problem because that's what I see when I browse throug the questions, and I tend to answer according to that. But let's end that pointless discussion ... – derpirscher Jun 06 '22 at 13:03
  • I apologize for not writing the title in detail. I will be more careful from now on. thanks for your answers – Taha Yıldırım Jun 06 '22 at 13:06
  • IMHO better title would be something like "How to remove elements with highest value of a property" or similar. Then of course, just pointing towards "sort" is not enough ... – derpirscher Jun 06 '22 at 13:15
  • @derpirscher yes, the title *is* a problem. As is the lack of clarity of what the end result should be: OP claims they want the top 10 (or whatever number) but in the output "C" is missing which is definitely the highest value from the input (sorting-wise). Since the body of the question is not quite ambiguous towards whether or not sorting is the only thing that's needed, we shouldn't be closing against the wrong target. – VLAZ Jun 06 '22 at 13:22

1 Answers1

0
const project = [
  {
    project_name: 'A',
    key1: 'value1',
  },
  {
    project_name: 'B',
    key1: 'value1',
  },
  {
    project_name: 'B',
    key1: 'value1',
  },
  {
    project_name: 'B',
    key1: 'value1',
  },
  {
    project_name: 'A',
    key1: 'value1',
  },
  {
    project_name: 'C',
    key1: 'value1',
  },
];

project.sort((a, b) => b.project_name.localeCompare(a.project_name));


const filtered = project.filter(({ project_name }) => {
  const highest = project[0];
  return project_name !== highest.project_name;
});
  • The sorting does not handle equality. This can lead to odd or inconsistent results in rare occasions, depending on the environment and the dataset. Moreover, there is no real reason to try and implement a custom equality function for strings: `str1.localeCompare(str2)` is natively supported and guaranteed to be correct. – VLAZ Jun 06 '22 at 12:49
  • thanks for comment. `project.sort((a, b) => b.project_name.localeCompare(a.project_name));`. Is it okay with this? – cruelladevil Jun 06 '22 at 13:10
  • Yes, that is correct. – VLAZ Jun 06 '22 at 13:22
  • this sort operation ensures that the project_names are in alphabetical order, not by total. – Taha Yıldırım Jun 06 '22 at 13:23
  • @TahaYıldırım your question does not make it clear at all that you expected the projects to be sorted by number of occurences. It just says "*the 2 projects with the highest project_name*" which reads like "sort by reverse alphabetical order, get projects with the top two names". – VLAZ Jun 06 '22 at 13:36
  • My question is not very detailed, but not so much to be misunderstood. I wrote the sorting of projects with the highest project_name value. should i add a long json for you to understand clearly – Taha Yıldırım Jun 06 '22 at 13:39
  • @TahaYıldırım by *what* 'total`? And yes, your question is very unclear and even contradicts itself ... – derpirscher Jun 06 '22 at 14:08
  • For example, imagine that json has the following, xyz project has 45, abc project has 33, klm project has 98, qwe project has 23. I want to use the 2 with the highest value here (klm and xyz) and I don't want to take the rest. of course, as in json, first klm's and then xyz's – Taha Yıldırım Jun 06 '22 at 14:21
  • @TahaYıldırım: Citing from your question: "*I would like to sort the project_name values ​​starting from the ones with the highest value and not take anything after a certain value in such a data.*" vs "*I don't want to take the 2 projects with the highest project_name value and get the rest of the data.*" That's a contradiction. Furthermore, in your example you remove only *one* value, whereas your question states you don't want to to take the *two projects* with the highest project name ... – derpirscher Jun 06 '22 at 15:31
  • @TahaYıldırım And tbh: It's in your own interest to formulate your question as clearly as possible if you want to get any help. So if someone tells you, your question is somehow unclear, I'd suggest to to ask *what's* unclear, instead of just saying there is nothing unclear ... – derpirscher Jun 06 '22 at 15:47