You'll need to use long-term capital losses to offset long-term capital gains, then use short-term capital losses to offset short-term capital gains. If you then have one type of losses that exceed the same type of gains, you can use it to offset the other type of gains. For instance, if you had a net short-term loss of $1,000 and net long-term capital gains of $5,000, you could use the short-term loss to reduce your long-term gains by $1,000.
Offset limits and carry forward rules
You can only use capital losses to reduce your taxable income by $3,000 in a given year. However, you can carry forward losses into future years indefinitely.
For example, if you sold a stock and took a $12,000 loss, you could reduce your capital gains or taxable income by $3,000 this year and carry forward the remaining $9,000. You could then use your loss to lower your capital gains or taxable income by $3,000 for each of the next three years.