我有一个文章集,比如
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)
我想做的是:查询来源并按链接到该来源的文章数量对其进行排序。这有可能吗?
下面是我对队列拓扑排序算法的阅读,写在我的教科书中:
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 实现拓扑排序,但我想按原样实现以下内容:
我正在尝试对时间进行排序。但我无法按时间 (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) 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) 我想对这个列表进行排序 a = [31415926535897932384626433832795, 1, 3, 10, 3, 5]。为了减少时间复杂度,我想首先检查两个元素的长度是否相同。如果两者的长度不同,我将根据它们的长度交换它们,否则我将检查哪个数字更大并交换它们。我想使用.sort()具有名为key. 我可以使用,a.sort(key=len)但它仅适用于具有不同长度输入的测试用例。请帮我解决这个问题。
在下面的二分查找函数返回整数类型的值中,为什么我们要在递归调用时在 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) 新程序员来了!我正在实现选择排序,使用一个最大变量而不是通常的最小值,但我仍然想从最低到最高排序。下面的代码对列表进行了完美的排序,除了第一个值是一个非常大的负数......我可以修复什么以使其正确排序的任何提示?
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) 我有一个看起来像这样的数组
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
考虑具有唯一值的列:
df['something'].unique() =array(['aa','bb','a','c']).
Run Code Online (Sandbox Code Playgroud)
现在我想知道哪些项目以 a 开头。我的预期答案是
'aa','a'
Run Code Online (Sandbox Code Playgroud) 我正在尝试执行以下操作的 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) sorting ×10
c ×3
javascript ×3
python ×3
arrays ×2
list ×2
algorithm ×1
collections ×1
count ×1
graph-theory ×1
html ×1
mongoose ×1
objectid ×1
pandas ×1
python-3.x ×1
react-hooks ×1
reactjs ×1
split ×1