标签: sorting

猫鼬按不同集合中对象 id 的计数排序

我有一个文章集,比如

var articleSchema = new Schema(
{
    link: { type: String, required: true, unique: true  },
    title: { type: String },
    description: {type: String },
    source: { type: Schema.ObjectId, ref: 'sources' },
    … 
};
Run Code Online (Sandbox Code Playgroud)

Source 通过 objectId 链接到 Source 模式。

var sourceSchema = new Schema(
{
    name: { type: String, required: true, lowercase: true },
    short_handle: { type: String, required: true, lowercase: true, unique: true },
    website: { type: String, required: true, lowercase: true, unique: true },
    …
}
Run Code Online (Sandbox Code Playgroud)

我想做的是:查询来源并按链接到该来源的文章数量对其进行排序。这有可能吗?

sorting collections count mongoose objectid

0
推荐指数
1
解决办法
583
查看次数

在图中使用队列进行拓扑排序

下面是我对队列拓扑排序算法的阅读,写在我的教科书中:

void topologicalsort(struct Graph* G){
    struct queue* Q;
    int counter;
    int v,w;
    Q=createqueue();
    counter=0;
    for(v=0;v<G->V;v++){
       if(indegree[v]==0)
          enqueue(Q,v);
       while(!isemptyqueue(Q)){
          v=dequeue(Q);
          topologicalorder[v]=++counter;
          for each w to adjacent to v
             if(--indegree[w]==0){
             enqueue(Q,w);
             }


       }
    } 
} 
Run Code Online (Sandbox Code Playgroud)

对于下图,该算法失败:

图算法失败

如果在给定的图中最初 7 5 3 的入度为零,那么它们将被插入到队列中,但是对于与7 5 3我们相邻的任何顶点,我们都没有任何度为 1 的顶点。这意味着这if(--indegree[w]==0)将不成立,7 5 3因此有将不会在队列内进一步排队,因此该算法将不会处理更多的顶点。如果图形是 DAG,我想知道为什么算法会失败?哪种方式不正确?

我知道我们也可以使用 DFS 实现拓扑排序,但我想按原样实现以下内容:

我要实现的算法的伪代码

c sorting algorithm graph-theory depth-first-search

0
推荐指数
1
解决办法
1840
查看次数

数组 按时间排序 hh:mm:ss

我正在尝试对时间进行排序。但我无法按时间 (hh:mm:ss) 格式排序。所以我使用了 moment js。我的数组按时间排序未排序。如何使用映射对数组进行排序

我有一个对象数组:

let elements =[
  {
    "id": 1,
    "date": "02:01:02"
  },
  {
    "id": 2,
    "date": "01:01:01"
  },
  {
    "id": 3,
    "date": "03:01:01"
  },
  {
    "id": 4,
    "date": "04:01:01"
  }
 ]; 


 let parsedDates = new Map(
        elements.map(e =>[["id", "date"],[e.id, moment(e.date, 'hh:mm:ss')]])
        );

    elements.sort((a, b) => parsedDates.get(a) - parsedDates.get(b));

    console.log(elements.map(e => ({ id: e.id, date: e.date })));
Run Code Online (Sandbox Code Playgroud)

javascript arrays sorting

0
推荐指数
1
解决办法
1463
查看次数

Python中的范围排序列表

rangeList = [range(15, 20), range(7, 10), range(11, 14)]
Run Code Online (Sandbox Code Playgroud)

如何对 进行排序rangeList,使结果列表如下所示,根据范围的起始值进行排序?

sortedRangeList = [range(7, 10), range(11, 14), range(15, 20)]
Run Code Online (Sandbox Code Playgroud)

python sorting list python-3.x

0
推荐指数
1
解决办法
800
查看次数

如何在时间复杂度较低的python中使用排序功能对列表进行排序?

我想对这个列表进行排序 a = [31415926535897932384626433832795, 1, 3, 10, 3, 5]。为了减少时间复杂度,我想首先检查两个元素的长度是否相同。如果两者的长度不同,我将根据它们的长度交换它们,否则我将检查哪个数字更大并交换它们。我想使用.sort()具有名为key. 我可以使用,a.sort(key=len)但它仅适用于具有不同长度输入的测试用例。请帮我解决这个问题。

python sorting list

0
推荐指数
1
解决办法
121
查看次数

为什么在使用递归调用时要使用 return 语句?

在下面的二分查找函数返回整数类型的值中,为什么我们要在递归调用时在 else if 语句中使用返回值?如果我直接使用递归调用而不是作为回报使用它,代码将不起作用。

int binarysearch(int l,int h,int key)
{
  int mid;
  mid=(l+h)/2;
  if(l<=h)                             
  {
    if(key==a[mid])
      return mid;
    else if(key>a[mid])
      return(binarysearch(mid+1,h,key));
    else if(key<a[mid])
      return(binarysearch(l,mid-1,key));
  }
  else
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

c sorting

0
推荐指数
1
解决办法
211
查看次数

选择排序在C中设置错误的值

新程序员来了!我正在实现选择排序,使用一个最大变量而不是通常的最小值,但我仍然想从最低到最高排序。下面的代码对列表进行了完美的排序,除了第一个值是一个非常大的负数......我可以修复什么以使其正确排序的任何提示?

void selection(int Array[], int size) {
   int i, j, max, temp;
   for (i = 0; i < size-1; i++) {
     max = i;
       for (j = i+1; j <= size; j++) {
          if ( Array[j] < Array[max] )
             max = j;
       }
       temp = Array[max];
       Array[max] = Array[i];
       Array[i] = temp;
   }
}
Run Code Online (Sandbox Code Playgroud)

c sorting selection-sort

0
推荐指数
1
解决办法
45
查看次数

使用拆分函数对 Javascript 数组进行排序

我有一个看起来像这样的数组

var testArray = ['name1:13', 'name2:15', 'name3:13'];
Run Code Online (Sandbox Code Playgroud)

我想按冒号右侧的数字对数组进行排序。

到目前为止,我有这个:

var converted = testArray.map(
            function (item) {
                return item.split(':').map(
            function (num) {
                return parseInt(num);
          });
        })

        alert(converted)
        var sorted = converted.sort(function (a, b) { return a[1] - b[1] })
        alert(sorted);
Run Code Online (Sandbox Code Playgroud)

这以正确的顺序对它们进行排序,但我不确定如何传递每个字符串的第一部分,即冒号左侧的部分。

现在它返回: NAN,13,NAN,13,NAN,15

html javascript sorting split

0
推荐指数
1
解决办法
338
查看次数

获取以给定字符串开头的列中的唯一项

考虑具有唯一值的列:

df['something'].unique() =array(['aa','bb','a','c']).
Run Code Online (Sandbox Code Playgroud)

现在我想知道哪些项目以 a 开头。我的预期答案是

'aa','a'
Run Code Online (Sandbox Code Playgroud)

python arrays sorting pandas

0
推荐指数
1
解决办法
82
查看次数

React/react hooks:子组件在状态改变后不重新渲染?

我正在尝试执行以下操作的 react/react 钩子编写代码。

从父组件获取对象数组作为 prop 使用useState钩子将其设置为状态。根据预期的过滤器(时间和评级)对状态进行排序,并重新渲染子组件。我看到的是,下面的代码在排序后更新了状态,但是即使状态更新了,依赖于该状态的子组件也不会重新渲染。我认为子组件会在状态改变时自动重新渲染?

import React, {useState} from 'react';
import ProfilePostspreview from './ProfilePostspreview';

function ProfileNavigation(props){
   const [newarray, setnewarray]=useState(props.parray);   //'parray' object passed as props and saved as 'newarray' state    
   const otc = () => {  //Function to sort the state by time and set a new state
       let k=newarray;
       setnewarray(k.sort((a, b) => (a.time > b.time) ? -1 : 1 ));
   }
   const orc = () => {  //Function to sort the state by rating and then time …
Run Code Online (Sandbox Code Playgroud)

javascript sorting reactjs react-hooks

0
推荐指数
1
解决办法
2371
查看次数